MAT668

Lab 1


In [0]:
%pylab inline


Populating the interactive namespace from numpy and matplotlib

Q1

Assign your name to a variable called "my_name".
Print your name using "print" function.
https://docs.python.org/3/tutorial/introduction.html#strings.


In [0]:


Q2

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

https://docs.python.org/3/tutorial/introduction.html#lists
https://docs.python.org/3/tutorial/controlflow.html#the-range-function


In [0]:


Q3

Define a list 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.


In [0]:


Q4

Use "zip" function 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 to convert "edges" sequence into a list.
Print the list of edges using "print" function.
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)]

https://docs.python.org/3/tutorial/datastructures.html#nested-list-comprehensions


In [0]:


Q5

Import networkx module as nx.

Then, create an empty graph G with no nodes and no edges.

https://networkx.github.io/documentation/stable/tutorial.html#creating-a-graph


In [0]:


Q6

Add "edges" to G using "add_edges_from" function.

https://networkx.github.io/documentation/stable/tutorial.html#edges


In [0]:


Q7

Print all nodes using "list" and print" function.

https://networkx.github.io/documentation/stable/tutorial.html#edges


In [0]:


Q8

Print all edges using "list" and print" function.

https://networkx.github.io/documentation/stable/tutorial.html#edges


In [0]:


Q9

Print the number of nodes using
"number_of_nodes" functon, and
"print" function.

https://networkx.github.io/documentation/stable/tutorial.html#edges


In [0]:


Q10

Print the number of edges using
"number_of_edges" function , and
"print" function.


In [0]:


Q11

Print the list of nodes adjacent to node 2 (neighbour of node 2)
using "neighbors" function
and "print" function.

https://networkx.github.io/documentation/stable/tutorial.html#edges


In [0]:


Q12

Print all the nodes and a list of their respective neighbours side by side using
"for loop",
"nodes" function,
"neighbors" function, and
"print" function.

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]

https://docs.python.org/3/tutorial/controlflow.html#for-statements
https://networkx.github.io/documentation/stable/tutorial.html#edges


In [0]:


Q13

Print the degree for node 2 using the "degree" function , and
"print" function.
Please ignore parameter "weight".

https://networkx.github.io/documentation/stable/tutorial.html#edges


In [0]:


Q14

Print all the nodes and their respective degree side by side using
"for loop",
"nodes" function,
"degree" function, and
"print" function.

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

https://docs.python.org/3/tutorial/controlflow.html#for-statements
https://networkx.github.io/documentation/stable/tutorial.html#edges



In [0]:


Q15

"G.degree()" is of type dict.
"G.degree().keys()" will return all the nodes.
"G.degree().values()" will return all the degrees.

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

Print "degree_sequence" using "print" function.

Note:

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

https://docs.python.org/3/tutorial/introduction.html#lists
https://docs.python.org/3/tutorial/datastructures.html#dictionaries


In [0]:


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.


In [0]:


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.


In [0]:


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.

Example:
Sum of degree of all nodes = 18


In [0]:


Q19

Calculate the value of 2 times the total number of edges using
"number_of_edges" function, and
assign it to a variable called "edges_times2".

Print "edges_times2" using "print" function.

Example:
edges_times2 = 2 x number_of_edges = 18

https://networkx.github.io/documentation/stable/tutorial.html#edges


In [0]:


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.


In [0]:


Q21

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


In [0]: