0% found this document useful (0 votes)
142 views1 page

Lab Activity 15 - Graphs

The document describes an activity where students will create and manipulate graph data structures. It lists 12 functions to implement: 1) add nodes to a graph, 2) add edges to a graph, 3) get a list of nodes, 4) get a list of edges, 5) print in and out degrees of nodes, 6) get neighbors of a node, 7) get nearest neighbor, 8) check for mutual neighbors, 9) remove a node, 10) remove nodes, 11) display the graph adjacency list, and 12) display the graph adjacency matrix. The objective is for students to build methods for creating and manipulating graph data.

Uploaded by

Adeena Saifullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
142 views1 page

Lab Activity 15 - Graphs

The document describes an activity where students will create and manipulate graph data structures. It lists 12 functions to implement: 1) add nodes to a graph, 2) add edges to a graph, 3) get a list of nodes, 4) get a list of edges, 5) print in and out degrees of nodes, 6) get neighbors of a node, 7) get nearest neighbor, 8) check for mutual neighbors, 9) remove a node, 10) remove nodes, 11) display the graph adjacency list, and 12) display the graph adjacency matrix. The objective is for students to build methods for creating and manipulating graph data.

Uploaded by

Adeena Saifullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

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  ----------------------------------------

You might also like