Group C - 6
Group C - 6
6 Group C
Problem Statement : C- 13 Represent a given graph using adjacency matrix/list to perform DFS and using
adjacency list to perform BFS. Use the map of the area around the college as the graph. Identify the
prominent land marks as nodes and perform DFS and BFS on that.
Learning Objectives:
To understand concept of Graph and Adjacency matrix.
To analyze the working of various Traversals-depth first and breadth first.
Learning Outcome: Students will be able to use various set of operations on depth first and breadth first.
Theory:
Graph:
2. A finite set of ordered pair of the form (u, v) called as edge. The pair is ordered because (u, v)
is not same as (v, u) in case of directed graph(di-graph). The pair of form (u, v) indicates that there
is an edge from vertex u to vertex v. The edges may contain weight/value/cost.
Graphs are used to represent many real life applications: Graphs are used to represent networks.
The networks may include paths in a city or telephone network or circuit network. Graphs are
also used in social networks like linkedIn, facebook. For example, in facebook, each person is
represented with a vertex(or node). Each node is a structure and contains information like person
id, name, gender and locale. See this for more applications of graph.
1. Adjacency Matrix
2. Adjacency List
There are other representations also like, Incidence Matrix and Incidence List. The choice of the
graph representation is situation specific. It totally depends on the type of operations to be
performed and ease of use.
Adjacency Matrix:
Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in a graph. Let
the 2D array be adj[][], a slot adj[i][j] = 1 indicates that there is an edge from vertex i to vertex j.
Adjacency matrix for undirected graph is always symmetric. Adjacency Matrix is also used to
represent weighted graphs. If adj[i][j] = w, then there is an edge from vertex i to vertex j with
weight w.
Pros: Representation is easier to implement and follow. Removing an edge takes O(1) time.
Queries like whether there is an edge from vertex ‘u’ to vertex ‘v’ are efficient and can be done
O(1).
Cons: Consumes more space O(V^2). Even if the graph is sparse(contains less number of edges),
it consumes the same space. Adding a vertex is O(V^2) time.
Adjacency List:
An array of linked lists is used. Size of the array is equal to number of vertices. Let the array be
array[]. An entry array[i] represents the linked list of vertices adjacent to the ith vertex. This
representation can also be used to represent a weighted graph. The weights of edges can be stored
in nodes of linked lists. Following is adjacency list representation of the above graph.
Pros: Saves space O(|V|+|E|) . In the worst case, there can be C(V, 2) number of edges in a graph
thus consuming O(V^2) space. Adding a vertex is easier.
Cons: Queries like whether there is an edge from vertex u to vertex v are not efficient and can be
done O(V).
Example:
Input: n = 4, e = 6
0 -> 1, 0 -> 2, 1 -> 2, 2 -> 0, 2 -> 3, 3 -> 3
Output: DFS from vertex 1 : 1 2 0 3
Step 2: Visit 0 and put its adjacent nodes which are not visited yet into the stack.
Step 3: Now, Node 1 at the top of the stack, so visit node 1 and pop it from the stack and put all of its adjacent
nodes which are not visited in the stack.
Step 4: Now, Node 2 at the top of the stack, so visit node 2 and pop it from the stack and put all of its adjacent
nodes which are not visited (i.e, 3, 4) in the stack.
Step 5: Now, Node 4 at the top of the stack, so visit node 4 and pop it from the stack and put all of its adjacent
nodes which are not visited in the stack.
Step 6: Now, Node 3 at the top of the stack, so visit node 3 and pop it from the stack and put all of its adjacent
nodes which are not visited in the stack.
Now, Stack becomes empty, which means we have visited all the nodes and our DFS traversal ends.
Outcome
Understand & implement different operations on Graph and traversal of DFS and BFS.
Questions:
1. An undirected graph having n edges, then find out no. Of vertices that graph have?
Program Code:
#include <iostream>
#include <list>
#include <queue>
#include <vector>
using namespace std;
class Graph {
int V; // Number of vertices
list<int> *adjList; // Pointer for adjacency list
vector<vector<int>> adjMatrix; // Adjacency matrix
public:
Graph(int V) {
this->V = V;
adjList = new list<int>[V];
adjMatrix.resize(V, vector<int>(V, 0));
}
// DFS traversal
void DFS(int v) {
vector<bool> visited(V, false);
DFSUtil(v, visited);
}
// BFS traversal
void BFS(int s) {
vector<bool> visited(V, false);
queue<int> queue;
visited[s] = true;
queue.push(s);
while (!queue.empty()) {
s = queue.front();
cout << s << " ";
queue.pop();
int main() {
Graph g(5); // Create a graph with 5 nodes (0 to 4)
g.addEdgeList(0, 1);
g.addEdgeList(0, 2);
g.addEdgeList(1, 2);
g.addEdgeList(1, 3);
g.addEdgeList(2, 4);
g.addEdgeList(3, 4);
g.addEdgeMatrix(0, 1);
g.addEdgeMatrix(0, 2);
g.addEdgeMatrix(1, 2);
g.addEdgeMatrix(1, 3);
g.addEdgeMatrix(2, 4);
g.addEdgeMatrix(3, 4);
return 0;
}