Lab Activity 15
Graphs
Objectives: In this lab students will build elementary methods to create and manipulate graph
data structure.
Question:
Use adjacency list to create graph G.
Create following functions:
1. addNodes(G,nodes): This function will take a graph G and a list of nodes as
parameters. It will add the nodes in the list in G and return the Updated Graph.
2. addEdges(G,edges, directed = False): This function will take a graph G and a list of
edges E as parameters. It will add the edges in the graph G and return the Updated
Graph.
Format of list of edges:
edges = [(u1 , v1 , w1) , (u2 , v2 , w2),…]
here, u = start node, v = end node, w = weight (for unweighted graph default value of w
is 1.)
3. listOfNodes(G): This function will take a graph G as a parameter and return a List of the
Nodes in the graph G.
4. listOfEdges(G, , directed = False): This function will take a graph G as a parameter
and return a List of the Edges in the graph G.
5. printInDegreeOutDegree(G): This function will take a graph G as a parameter and print
InDegree and Out Degree for each node in the Graph.
6. getNeighbors (G,node) - The function will take a graph G and a node as a parameter
and will return its neighboring nodes.
7. getNearestNeighbor(G,node) The function will take a weighted graph G and a node
and return its nearest neighboring nodes.
8. mutualNeighbors(G ,Node1, Node2) : The function will take a directed graph G ,
Node1 and Node2 and return True if both Node 1 and Node 2 are neighbors of each
other, False otherwise.
9. removeNode(G,node): This function will take a graph G and a node as a parameter. It
will remove that node and all edges of that node and return the Updated Graph.
10. removeNodes(G,nodes): This function will take a graph G and a list of nodes as
parameters.It will remove the nodes in the list in G and return the Updated Graph.
11. displayGraph(G): This function will take a graph G as a parameter and print Adjacency
list representation of the graph G.
12. display_adj_matrix(G): This function will take a graph G (adjacency list) as a parameter
and
print the adjacency matrix representation of the graph G.
---------------------------------------- Good Luck ----------------------------------------