MAT668

Lab 1


In [195]:
%pylab inline


Populating the interactive namespace from numpy and matplotlib

Q1

Assign your name to a variable called "my_name" (Python Tutorial, pg. 8).
Print your name using "print" function (Python Tutorial, pg. 9).


In [ ]:


Q2

Define a list called "nodes" of 10 nodes (0, 1, ..., 9) using "range" function (Python Tutorial, pg. 16).
Convert the range into a list using "list" function (Python Tutorial, pg. 12).
Print the list of nodes using "print" function (Python Tutorial, pg. 9).


In [ ]:


Q3

Define a list (Python Tutorial, pg. 12) using your student ID named "studid".
For example, if your student ID is "2015234031",
your list is [2, 0, 1, 5, 2, 3, 4, 0, 3, 1].
Print it using "print" function (Python Tutorial, pg. 9).


In [ ]:


Q4

Use "zip" function (Python Tutorial, pg. 67) to create a sequence of tuple and called it "edges".
Each tuple defines an edge between two nodes from the list nodes
and the list studid (nodes[i], stud[i]).
Then, use "list" function (Python Tutorial, pg. 12) to convert "edges" sequence into a list.
Print the list of edges using "print" function (Python Tutorial, pg. 9).
Example:
If nodes = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] and studid = [2, 0, 1, 5, 2, 3, 4, 0, 3, 1]
then, edges = [(0, 2), (1, 0), (2, 1), (3, 5), (4, 2), (5, 3), (6, 4), (7, 0), (8, 3), (9, 1)]


In [ ]:


Q5

Import networkx module as nx.

Then, create an empty graph G with no nodes and no edges (NetworkX Tutorial pg. 1).


In [ ]:


Q6

Add "edges" to G using "add_edges_from" function (NetworkX Tutorial pg. 2).


In [ ]:


Q7

Print all nodes (NetworkX Tutorial pg. 3) using "print" function (Python Tutorial, pg. 9).


In [ ]:


Q8

Print all edges (NetworkX Tutorial pg. 3) using "print" function (Python Tutorial, pg. 9).


In [ ]:


Q9

Print the number of nodes using
"number_of_nodes" functon (NetworkX Tutorial pg. 3), and
"print" function (Python Tutorial, pg. 9).


In [ ]:


Q10

Print the number of edges using
"number_of_edges" function (NetworkX Tutorial pg. 3), and
"print" function (Python Tutorial, pg. 9).


In [ ]:


Q11

Print the list of nodes adjacent to node 2 (neighbour of node 2)
using "neighbors" function (NetworkX Tutorial pg. 3)
and "print" function (Python Tutorial, pg. 9).


In [ ]:


Q12

Print all the nodes and a list of their respective neighbours side by side using
"for loop" (Python Tutorial, pg. 15),
"nodes" function (NetworkX Tutorial pg. 3),
"neighbors" function (NetworkX Tutorial pg. 3), and
"print" function (Python Tutorial, pg. 9).

Example:
0 [1, 2, 7]
1 [0, 9, 2]
2 [0, 1, 4]
3 [8, 5]
4 [2, 6]
5 [3]
6 [4]
7 [0]
8 [3]
9 [1]


In [ ]:


Q13

Print the degree for node 2 using the "degree" function (NetworkX Tutorial pg. 5), and
"print" function (Python Tutorial, pg. 9).
Please ignore parameter "weight".


In [ ]:


Q14

Print all the nodes and their respective degree side by side using
"for loop" (Python Tutorial, pg. 15),
"nodes" function (NetworkX Tutorial pg. 3),
"degree" function (NetworkX Tutorial pg. 5), and
"print" function (Python Tutorial, pg. 9).

Example:
0 3
1 3
2 3
3 2
4 2
5 1
6 1
7 1
8 1
9 1



In [ ]:


Q15

"G.degree()" is of type dict (Python Tutorial, pg. 31).
"G.degree().keys()" will return all the nodes.
"G.degree().values()" will return all the degrees.
Read the Python Tutorial, pg. 31 on how to convert them to a list.

Create a degree sequence in descending order using
"G.degree().values()" function and "list" function (Python Tutorial, pg. 12)
and assign it to variable called "degree_sequence"

Print "degree_sequence" using "print" function (Python Tutorial, pg. 9).

Note:

Example:
[3, 3, 3, 2, 2, 1, 1, 1, 1, 1]


In [ ]:


Q16

Find the maximum vertex degree $\Delta$ by applying
"max" function to the list "degree_sequence"
and assign it to variable "max_degree".

Print variable "max_degree" using "print" function (Python Tutorial, pg. 9).


In [ ]:


Q17

Find the minimum vertex degree $\delta$ by applying
"min" function to the list "degree_sequence"
and assign it to variable "min_degree".

Print variable "min_degree" using "print" function (Python Tutorial, pg. 9).


In [ ]:


Q18

Calculate the sum of degree of all nodes

"sum" function to calculate the sum of all degree, and
assign it to a variable called "sum_degree".

Print "sum_degree" using "print" function (Python Tutorial, pg. 9).

Example:
Sum of degree of all nodes = 18


In [ ]:


Q19

Calculate the value of 2 times the total number of edges using
"number_of_edges" function (NetworkX Tutorial pg. 3), and
assign it to a variable called "edges_times2".

Print "edges_times2" using "print" function (Python Tutorial, pg. 9).

Example:
edges_times2 = 2 x number_of_edges = 18


In [ ]:


Q20

Verify Handshaking Lemma (The degree sum formula).
$\sum_{v \in V} deg(v) = 2|E|$

Hint:
Is sum_degree = edges_times2?
Print the value using "print" function (Python Tutorial, pg. 9).


In [ ]:


Q21

Draw the graph G using "draw" function. Add "with_labels=True" argument to "draw" function to display the node's label.


In [ ]: