Class_ppt1_Graphs
Class_ppt1_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
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.
1 n = 4, m = 5
4
3
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?
1 9
5
8
2
13 10 11
3 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
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;
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.
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
}
}
}
calls
• 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.
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
1 35 524
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.
1 9
5
8
2
13 10 11
3 4
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.
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.