0% found this document useful (0 votes)
10 views22 pages

Class_ppt1_Graphs

The document provides an overview of graphs, defining key concepts such as vertices, edges, and classifications of graphs (directed vs. undirected). It explains graph traversal algorithms, specifically Breadth First Search (BFS) and Depth First Search (DFS), detailing their implementations, applications, and complexities. The document emphasizes the importance of these algorithms in computing distances, checking connectivity, and other graph-related problems.

Uploaded by

parthratna2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views22 pages

Class_ppt1_Graphs

The document provides an overview of graphs, defining key concepts such as vertices, edges, and classifications of graphs (directed vs. undirected). It explains graph traversal algorithms, specifically Breadth First Search (BFS) and Depth First Search (DFS), detailing their implementations, applications, and complexities. The document emphasizes the importance of these algorithms in computing distances, checking connectivity, and other graph-related problems.

Uploaded by

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

Graphs

Prof. Varsha Hole


Why Graphs?

For example, given a network of cities and roads connecting them, what is the
shortest path between two given cities?
7
B D B
7
D
10 10
3 3
A 15
A 15

4 4
C 8 E C 8 E

Shortest path between A and D?

This is a very natural way to model and think of the given data.

Today, we’ll look at this in detail and formalize some notions about this data
structure!
Graphs
A graph G is defined as G = (V, E).
Here, V is a set of vertices and E is a set of edges. Edges may or may not have
weights.
Weights
(optional)
Vertices
7
B D
10
3
A 15

4
C E
8

Edges
Classifications of graphs
Undirected Graph Directed Graph
B B

A A
D D
C C

V = {A, B, C, D} V = {A, B, C, D}
E = { (A, B), (A,C), E = { (A, B), (A,C),
(B,D), (B,C), (C, D) } (B,D), (C,B), (D, C) }

Note: E is a set. Therefore, in an undirected graph, we write only one of (A,C) and
(C, A) since they are the same edge. This is not the case for a directed graph.

In general, edge sets cannot tell you whether the graph is directed or undirected.
Rather, knowing about the graph tells you how to interpret the edge set.
Graph Terminologies
• Walk:
A walk from x to y is a sequence of vertices <x, v0, v1, …, vk, y> such that
o (x, v0) ϵ E
o (vk, y) ϵ E
o For all i = 0 to k-1, (vi, vi+1) ϵ E
• Path:
A path is a walk <x, v0, v1, …, vk, y> where no vertex gets repeated.

• Cycle:
A cycle is a walk <x, v0, v1, …, vk, y> where no intermediate vertex gets repeated and
y=x
Examples:
<A, B, C, D, B, A, C> is a walk.
B
<A, B, D> is a path.
A
D
<A, B, D, C, A> is a cycle.
C
<B, C, D, A> is not a walk. Why?
Representations of Graphs
Suppose there are n vertices in the graph. This gives us the vertex set {1, …, n}. Now
we need to store the edge set. Here are two ways of doing this.

Adjacency Matrix representation

• We store an n × n size 2D array A (a matrix, essentially).


• Ai,j = 1 iff there is an edge between i and j in the graph.
• Size of the representation is O(n2).

Adjacency List representation

• We store a linked list for every vertex.


• The linked list for vertex i consists of the neighbours of i in the graph.
• Size of the representation is O(n + m) where m is the number of edges in the
graph.

Let us look at examples of these now.


Representations of Graphs
2

1 n = 4, m = 5
4
3

Adjacency matrix: Adjacency list:


1 2 3 4
1 2 3 X
1 0 1 1 0
2 1 3 4 X
2 1 0 1 1
3 1 1 0 1 3 1 2 4 X
4 0 1 1 0
4 2 3 X
Implementations of Graph Data Structures
Adjacency matrix: Use a n x n matrix. Ai, j = weight of edge from node i to node j.
Adjacency list:

There are also other implementations of Adjacency Lists.


An array of linked lists is a basic implementation, but it requires careful manipulation of
pointers and good knowledge of the workings linked lists and structures.
What is the set of Graph Traversal
vertices reachable
from 1?
6
1 7
5 9
2
3 4 8 10 11

A Isvertex
thereyaispath
saidbetween
to be reachable
711and
and fromNo!
and11?
8?
4? vertex x if there exists a path from x to y.
Yes!
Question: Given a vertex x, can we visit all vertices reachable from x?

Graph Traversal at x: Visit all vertices reachable from x.

How do we perform graph traversal?


Breadth First Search – A Graph Traversal Algorithm
Look at neighbours of 1 7
6 12

1 9
5
8
2
13 10 11
3 4

Enqueue 3, 5 Enqueue 2,4

1 35 524
EnqueueDequeue
8 Enqueue 7 Enqueue 6
Dequeue 1 and look at 3 and look at Dequeue 5 and look at
neighbours neighbours of 3 neighbours
678 467 246

Dequeue 6 and look at Dequeue 4 and look at Dequeue 2 and look at


7 8 neighbours 8 neighbours neighbours

Dequeue 7 and Neighbours


look at We cannot enqueue any neighbour! at Also, no need
Imporant: of 3 are 1, 2, Dequeue
4.
to But we
update
8 have
and look
visitedalready
The array.
only visited
unvisited 1! Our queue1,isthen
If we enqueue
neighbour is
nowweempty.
will
neighbours The only unvisited
neighbours neighbour is Mark
We 2, 4 as
terminatevisited!
the algorithm here.
3 All
end up in a loop. neighbours
So
Check
is We we need
visited
visited!
can now visited!
the
array! crucial
check Mark 6 as
information
unvisited Mark
7visited!
isthat
neighbours unvisited!
7 as
wevisited!
Mark
of have
3. visited 1, in order to avoid
88!as visited!
looping! For this, we need to keep another array 6! called visited.
1 2 3 4 5 6 7 8 9 10 11 12 13

1 0
1 1 0
1 1 1
0 1
0 1
0 0 0 0 0 0
Details on Breadth First Search
• BFS from 1 visited all vertices reachable from 1. Also, it visited only reachable vertices from 1.
• Before enqueing, we check if the vertex is visited. And after we enqueue, we mark it visited. So a
vertex enters the queue at most once. This is essential to ensure termination.
• We look at all and only unvisited neighbours while processing a vertex.
• BFS from 1 visits vertices in increasing order of distance.
BFS(G, x) //BFS on graph G, starting at vertex x. Distance between x and y is the number
of edges in the shortest path from x to y.
Create empty queue Q.
Create array visited of length n=|V| and initialize to 0.
Enqueue(x,Q); visited[x] = 1; //BFS on graph G, starting at vertex x.
while( Q is not empty)
{
v ←DeQueue(Q);
For each neighbor w of v
{
if(visited[w] == 0)
{ Enqueue(w,Q);
visited[w] = 1;
}
}
}
Breadth First Search - Implementation
// Define the visited array initialized to 0.
vector<int> visited(n, 0);
deque<int> Q;

// Function to perform BFS starting from x


void BFS(int x)
{
// Enqueue x and mark it visited!
Q.push_back(x);
visited[x]=1;
while(!(Q.empty())) //While the queue is not empty
{
// Dequeue operation.
int v = Q.pop_front();
num_v = adj[v].size();
for(int i=0; i<num_v; i++)
{
int w = adj[v][i];
if(visited[w] == 0) Check if w is visited.
{
Q.push_back(w); Enqueue w and mark it visited.
visited[w] = 1;
}
}
}
}

After BFS(x) terminates, for all vertices y, visited[y] = 1 iff y is reachable from x.
Breadth First Search - Applications
• Computing Distances: Given a source vertex x, compute the distance of all vertices from
x.

• Checking for cycles in a graph: Given an undirected graph G, report whether there exists
a cycle in the graph or not. (Note: won’t work for directed graphs)

• Checking for bipartite graph: Given a graph, check whether it is bipartite or not? A
graph is said to be bipartite if there is a partition of the vertex set V into two sets V 1 and
V2 such that if two vertices are adjacent, either both are in V 1 or both are in V2.

• Reachability: Given a graph G and vertices x and y, determine if there exists a path from
x to y.

• For more applications, visit


http://www.geeksforgeeks.org/applications-of-breadth-first-traversal/
Breadth First Search – Time and Space Complexity
BFS(G, x) //BFS on graph G, starting at vertex x.
Create empty queue Q.
Create array visited of length n=|V| and initialize to 0. O(n)
Enqueue(x,Q); visited[x] = 1; //BFS on graph G, starting at vertex x.
while( Q is not empty)
{
v ←DeQueue(Q);
For each neighbor w of v
{
if(visited[w] == 0) O(deg(v))
{ Enqueue(w,Q);
O(1)
visited[w] = 1;
}
}
}

We keep a queue and a visited array. This takes O(n) space at most.
Each node enters queue at most once. Time complexity of BFS traversal is O(n + m).
Total space requirement for BFS is therefore O(n).
Depth First Search - Another Traversal algorithm
Nowhere
Backtrack!
to go.
7
6 12

1 9
5
8
2
13 10 11
3 4
4 is unvisited neighbour!

Need visited
But how doarray again!
we know?
• The simple idea is : Keep going as “deep” into the graph as possible. This
is done by an unvisited neighbour and visiting it, if it hasn’t been visited.
• Once all neighbours of the current vertex x have been visited, the
control goes to the vertex y that called the procedure on x.
• Visited array needed to avoid looping!
• A naturally Recursive procedure due to backtracking.

Mark 4 as visited!
Mark 5 as visited! Mark 8 as visited!

1 2 3 4 5 6 7 8 9 10 11 12 13

1 1 1 0
1 0
1 1 1 0
1 0 0 0 0 0
Details on Depth First Search
DFS(x)
{
visited[x] = 1; // Mark x as visited.
For each neighbor w of v 7
6
{
1
if(visited[w] == 0) 5
{ 8
2
DFS(w); 3 4
}
}
}

returns returns returns returns


DFS(1) calls DFS(3) calls DFS(2) calls DFS(7) calls DFS(6)
returns

calls

Notice a tree-like structure emerging as a


returns returns result of DFS.
DFS(8) calls DFS(4) calls DFS(5) This is called the DFS Tree.
Depth First Search - Applications
• Computing Strongly Connected Components: A directed graph is strongly connected if
there exists a path from every vertex to every other vertex. Trivial Algo: Perform DFS n
times. Efficient Algo: Single DFS.

• Checking for Biconnected Graph: A graph is biconnected if removal of any vertex does
not affect connectivity of the other vertices. Used to test if network is robust to failure
of certain nodes. A single DFS traversal algorithm.

• Topological Ordering: Topological sorting for Directed Acyclic Graph (DAG) is a linear
ordering of vertices such that for every directed edge (x, y), vertex x comes before y in
the ordering.

• Plus, almost everything that BFS can do!

• For more applications,


visit http://www.geeksforgeeks.org/applications-of-depth-first-search/

In general, DFS is more powerful and more intuitive algorithm, than BFS!
DFS and BFS
Some Applications.
Breadth First Search – A Graph Traversal Algorithm
7
6 12

1 9
5
8
2
13 10 11
3 4

All Vertices at distance 0 All Vertices at distance 1

1 35 524

678 467 246


All Vertices at distance 2
78 8

All Vertices at distance 3


Once we process and dequeue all vertices at distance d, the queue contains all and only
vertices at distance d+1.
1 2 3 4 5 6 7 8 9 10 11 12 13

1 0
1 1 0
1 1 1
0 1
0 1
0 0 0 0 0 0
Using BFS to compute distances
• BFS from 1 visits vertices in increasing order of distance.

BFS(G, x) //BFS on graph G, starting at vertex x.


Create empty queue Q.
Create array distance
visited ofoflength
lengthn=|V|
n=|V|andandinitialize
initializetoto0.∞.
Enqueue(x,Q); distance(x)
visited[x]==0;1;
while( Q is not empty)
{ Idea: We don’t need visited array
because unvisited nodes have
v ←DeQueue(Q);
distance ∞. So a distance array
For each neighbor w of v suffices!
{
ifif(visited[w]
(distance(w) ====
0)∞)
{
Enqueue(w,Q);
distance(w) = distance(v) + 1;
visited[w] = 1;
Enqueue(w,Q);
}
}
}

This is essentially a BFS so time complexity is still O(m + n).


Depth First Search - Another Traversal algorithm
7
6 12

1 9
5
8
2
13 10 11
3 4

• Keep going as “deep” into the graph as possible. This is done by an


unvisited neighbour and visiting it, if it hasn’t been visited.
• Once all neighbours of the current vertex x have been visited, the
control goes to the vertex y that called the procedure on x.
• Visited array needed to avoid looping!
• A naturally Recursive procedure due to backtracking.

1 2 3 4 5 6 7 8 9 10 11 12 13

1 1 1 0
1 0
1 1 1 0
1 0 0 0 0 0
Depth First Search - Another Traversal algorithm
Compute property at 7.

Want to compute property at 1 7


6
1
5
8
2
3 4
Compute property at 3.

Combine properties at 3 and 7 to compute property at 1. This is essentially done using recursion!
DFS is naturally recursive and therefore is a suitable choice in this case.

Some recursive properties:


• Height (or depth) of a node in a tree.
• DFN numbering.
• Size of subtree.
• Reachability.
• …. and many more.

You might also like