0% found this document useful (0 votes)
2 views

Graphs Practice Questions

The document outlines a homework assignment for a Data Structures course focused on building a graph class using adjacency list representation. It specifies the attributes and methods required for the graph and vertex classes, including functions for adding edges and vertices, performing depth-first and breadth-first searches, and checking graph properties like 2-colorability. Additionally, it emphasizes the importance of boundary checks in the implementation.

Uploaded by

Jasia Nisar
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)
2 views

Graphs Practice Questions

The document outlines a homework assignment for a Data Structures course focused on building a graph class using adjacency list representation. It specifies the attributes and methods required for the graph and vertex classes, including functions for adding edges and vertices, performing depth-first and breadth-first searches, and checking graph properties like 2-colorability. Additionally, it emphasizes the importance of boundary checks in the implementation.

Uploaded by

Jasia Nisar
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/ 2

GRAPH – HOMEWORK

Data Structures (Section A, B, C and D)


Fall 2019

1. Build a graph class for a simple graph that uses adjacency list representation. Simple means that between
any two vertices in a graph G, there will be at most one edge.

2. For each vertex you need to remember the vertex id, name, marker (for visited), list of adjacent nodes.
Make a class for vertex. A vertex will have following attributes:

a. Vertex Id: int


b. Vertex name: char or string (your choice).
c. Marker: a bool, it will keep track of the fact that vertex is visited or not. We added this as most of
the graph algorithms need to mark the vertex.
d. List of ids of adjacent nodes. Use std linked list.

3. Keep an array of vertices in graph class, an integer to store the number of vertices and a bool for
directed/undirected. Do not keep a linked list of vertices. Because that will increase the time complexity of
graph functions like DFS and BFS .

4. Provide following functions in your graph class


a. Constructor
b. Destructor
c. AddEdge(int vertexId1 , int vertexId2 )
i. Input two vertex ids and add an edge between them that is it will add vertex with vertexId2
at the end of vertexId1’s adjacency list if it is directed graph and Also add vertexId1 at the
end of vertexId2’s list if graph is undirected.
ii. You can assume that the vertex id is an index in the vertex array. That is vertexId=10 will be
at index 10 in the vertex array.

d. AddEdge(vertexname1 , vertexname2 )
i. Input the names of two vertices and add an edge between them
ii. Note in this case you will have to search the vertices with given name

e. AddVertex
i. The function input vertex name from the user
ii. The function assigns next available free vertex id to the vertex
iii. Set visited to false
iv. Set Adjacent nodes to Null. (because we will add adjacent nodes information using add
edge function
f. ResetMarker
i. It will reset the boolean marker in each vertex to zero.
g. Print the graph in Depth First order
i. print the name of the vertices in order they are visited in DFS
ii. Run the DFS
h. Print the graph in Breadth First order (print the name of the vertices in order they are visited in
DFS)
i. Print the DFS Spanning tree (just output the list of edges in the tree).
j. Print BFS Spanning tree (just output the list of edges in the tree).
k. Computes and return the square of a graph. The square of a graph G(V, E) is the graph G2 (V, E2)
such that (u, v) Є E2 if and only G contains a path with at most two edges between u and v. Write an
efficient function for computing G2 from G.
l. Compute and return sum of degrees of all the neighbors of each vertex. Let Nsum[v] be the sum of
degrees(out degrees) of all the neighbors of a vertex v in an undirected(directed) graph G(V, E).
Devise an O(|V|+|E|) function to compute the value of Nsum for each vertex in G.
m. Develop a function Is2Colorable that returns true if an undirected graph is 2-colorable and false
otherwise. An undirected graph G(V, E) is k-colorable if k different colors are enough to color the
vertices of the graph such that no two adjacent vertices share a common color. Below is an example
of 3-colorable graph. Hint: you can use BFS to solve this problem

Do include all the necessary boundary checks in your code. Such as an edge can only be added if a
vertex exists.
Template is not a requirement.

You might also like