DS Lecture Week 13
DS Lecture Week 13
2
Adjacency matrix
A diagrammatic representation of a graph may have limited usefulness. However, such a
representation is not feasible when number of nodes an edges in a graph is large
It is easy to store and manipulate matrices and hence the graphs represented by them in the
computer
Let G = (V, E) be a simple diagraph in which V = {v1, v2,…., vn} and the nodes are assumed to be
ordered from v1 to vn
An n x n matrix A is called Adjacency matrix of the graph G whose elements are aij are given by
1 𝑖𝑓 𝑉𝑖 , 𝑉𝑗 ∈ 𝐸
aij =
0 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
3
Adjacency matrix
An element of the adjacency matrix is either 0 or 1
Any matrix whose elements are either 0 or 1 is called bit matrix or Boolean matrix
For a given graph G =m (V, E), an adjacency matrix depends upon the ordering of the elements
of V
For different ordering of the elements of V we get different adjacency matrices.
V1 V4 V1 V2 V3 V4
V1 0 1 0 1
A = V2 1 0 0 0
V3 1 1 0 1
V4 0 1 0 0
V2 V3
4
Adjacency matrix
V1 V4 V1 V2 V3 V4
V1 0 1 0 1
A= V2 1 0 0 0
V3 1 1 0 1
V4 0 1 0 0
V2 V3
The number of elements in the ith row whose value is 1 is equal to the out-degree of node Vi
The number of elements in the jth column whose value is 1 is equal to the in-degree of node Vj
For a NULL graph which consist of only n nodes but no edges, the adjacency matrix has all its
elements 0. i.e. the adjacency matrix is the NULL matrix
5
Power of Adjacency matrix
1 1 0 1
0 1 0 1 1 1 0 0 1 1 0 0
1 0 0 0 0 1 0 1 A3 =
2 2 0 1
A= A2 = A x A =
1 1 0 1 1 2 0 1 0 1 0 1
0 1 0 0 1 0 0 0 1 2 0 1
1 1 0 1
A4 =
2 3 0 2
1 1 0 0
Entry of 1 in ith row and jth column of A shows existence of an edge (Vi, Vj), that is a path of length 1
Entry in A2 shows no of different paths of exactly length 2 from node Vi to Vj
Entry in A3 shows no of different paths of exactly length 3 from node Vi to Vj
6
Path matrix or reachability matrix
Let G = (V,E) be a simple diagraph which contains n nodes that are assumed to be ordered.
A n x n matrix P is called path matrix whose elements are given by
1, 𝑖𝑓 𝑡ℎ𝑒𝑟𝑒 𝑒𝑥𝑖𝑠𝑡𝑠 𝑝𝑎𝑡ℎ 𝑓𝑟𝑜𝑚 𝑛𝑜𝑑𝑒 𝑉𝑖 𝑡𝑜 𝑉𝑗
𝑃𝑖𝑗 =
0, 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
7
Adjacency List Representation
0
4
1
2
3
0 1 2 3 4
1 0 3
2 0 3 4
3 0 1 2 4
4 0 2 3
8
Graph Traversal
Two Commonly used Traversal Techniques are
Depth First Search (DFS)
Breadth First Search (BFS)
9
Depth First Search (DFS)
It is like preorder traversal of tree
Traversal can start from any vertex Vi
Vi is visited and then all vertices adjacent to Vi are traversed recursively using DFS
8 1 2 6 3 8 7 4 5
10
Depth First Search (DFS)
✓ A
✓ B C ✓
M N O
✓ D ✓ E F ✓ G ✓
R
Q P
H
✓
ABDHECFG
✓ A
✓
✓ B ✓ C E A B D C F E
✓ D F✓
11
Breadth First Search (BFS)
This methods starts from vertex V0
V0 is marked as visited. All vertices adjacent to V0 are visited next
Let vertices adjacent to V0 are V1, V2, V2, V4
V1, V2, V3 and V4 are marked visited
All unvisited vertices adjacent to V1, V2, V3, V4 are visited next
The method continuous until all vertices are visited
The algorithm for BFS has to maintain a list of vertices which have been visited but not
explored for adjacent vertices
The vertices which have been visited but not explored for adjacent vertices can be stored in
queue
12
Breadth First Search (BFS)
✓ 1 ✓ A
✓ ✓
2 5 ✓ B ✓ C
✓ ✓
✓
3 4 ✓ D ✓ E F✓ G
6 ✓ 7 ✓
H✓
✓ 8 1 |2 3 4 5| 6 7| 8 A|BC |DEFG |H
V4
V1
V0| V1 V2 | V4 V6 V3 | V5
V2
V0 V6
V3
V5
13
Write DFS & BFS of following Graphs
A
B C
M N O
D E F G
R
Q P
H
A A
0 1
B C E B E
5 2
D F
4 3 C D
14
Procedure : DFS (vertex V)
This procedure traverse the graph G in DFS manner.
V is a starting vertex to be explored.
Visited[] is an array which tells you whether particular vertex is visited or not.
W is a adjacent node of vertex V.
S is a Stack, PUSH and POP are functions to insert and remove from stack respectively.
15
Procedure : DFS (vertex V)
1. [Initialize TOP and Visited]
visited[] 0
TOP 0
2. [Push vertex into stack]
PUSH (V)
3. [Repeat while stack is not Empty]
Repeat Step 3 while stack is not empty
v POP()
if visited[v] is 0
then visited [v] 1
for all W adjacent to v
if visited [w] is 0
then PUSH (W)
end for
end if
16
Procedure : BFS (vertex V)
This procedure traverse the graph G in BFS manner
V is a starting vertex to be explored
Q is a queue
visited[] is an array which tells you whether particular vertex is visited or not
W is a adjacent node f vertex V.
17
Procedure : BFS (vertex V)
1. [Initialize Queue & Visited]
visited[] 0
F R 0
2. [Marks visited of V as 1]
visited[v] 1
3. [Add vertex v to Q]
InsertQueue(V)
4. [Repeat while Q is not Empty]
Repeat while Q is not empty
v RemoveFromQueue()
For all vertices W adjacent to v
If visited[w] is 0
Then visited[w] 1
InsertQueue(w)
18
Topological Sort
Bennett University
Gr. Noida
Ordering a graph
• Suppose we have a directed acyclic graph (DAG) of courses,
and we want to find an order in which the courses can be
taken.
• Must take all prereqs before you can take a given course. Example:
• [142, 143, 140, 154, 341, 374, 331, 403, 311, 332, 344,
312, 351, 333, 352, 373, 414, 410, 417, 413, 415]
• There might be more than one allowable ordering.
• How can we find a valid ordering 374
of the vertices?
311 373 414
143 351 352
332
312 344 417
331 333
341 140 413
410
403 142
154 415
Topological Sort
• topological sort: Given a digraph G = (V, E ), a total ordering
of G 's vertices such that for every edge (v, w) in E , vertex v
precedes w in the ordering. Examples:
• determining the order to recalculate updated cells in a spreadsheet
• finding an order to recompile files that have dependencies
• (any problem of finding an order to perform tasks with dependencies)
374 414
311 373
143 351 352
332
312 344 417
331 333
341 140 413
410
403 142
154 415
Topological sort example
• How many valid topological sort orderings can you find for the
vertices in the graph below?
• [A, B, C, D, E, F], [A, B, C, D, F, E],
• [A, B, D, C, E, F], [A, B, D, C, F, E], C
• [B, A, C, D, E, F], [B, A, C, D, F, E], F
B E
• [B, A, D, C, E, F], [B, A, D, C, F, E],
• [B, C, A, D, E, F], [B, C, A, D, F, E],
• ... D
A
Topological sort example
• function topologicalSort():
• ordering := { }.
• Repeat until graph is empty:
• Find a vertex v with in-degree of 0 (no incoming edges).
• (If there is no such vertex, the graph cannot be sorted; stop.)
• Delete v and all of its
outgoing edges from the graph. C
• ordering += v .
F
B E
• ordering = { B }
A
Topological sort example
• function topologicalSort():
• ordering := { }.
• Repeat until graph is empty:
• Find a vertex v with in-degree of 0 (no incoming edges).
• (If there is no such vertex, the graph cannot be sorted; stop.)
• Delete v and all of its
outgoing edges from the graph. C
• ordering += v .
F
B E
• ordering = { B, C }
A
Topological sort example
• function topologicalSort():
• ordering := { }.
• Repeat until graph is empty:
• Find a vertex v with in-degree of 0 (no incoming edges).
• (If there is no such vertex, the graph cannot be sorted; stop.)
• Delete v and all of its
outgoing edges from the graph. C
• ordering += v .
F
B E
• ordering = { B, C, A }
A
Topological sort example
• function topologicalSort():
• ordering := { }.
• Repeat until graph is empty:
• Find a vertex v with in-degree of 0 (no incoming edges).
• (If there is no such vertex, the graph cannot be sorted; stop.)
• Delete v and all of its
outgoing edges from the graph. C
• ordering += v .
F
B E
• ordering = { B, C, A, D }
A
Topological sort example
• function topologicalSort():
• ordering := { }.
• Repeat until graph is empty:
• Find a vertex v with in-degree of 0 (no incoming edges).
• (If there is no such vertex, the graph cannot be sorted; stop.)
• Delete v and all of its
outgoing edges from the graph. C
• ordering += v .
F
B E
• ordering = { B, C, A, D, F }
A
Topological sort example
• function topologicalSort():
• ordering := { }.
• Repeat until graph is empty:
• Find a vertex v with in-degree of 0 (no incoming edges).
• (If there is no such vertex, the graph cannot be sorted; stop.)
• Delete v and all of its
outgoing edges from the graph. C
• ordering += v .
F
B E
• ordering = { B, C, A, D, F, E }
A
Revised algorithm
• We don't want to literally delete vertices and edges from the
graph while trying to topological sort it; so let's revise the
algorithm:
• map := {each vertex → its in-degree}.
• queue := {all vertices with in-degree = 0}.
• ordering := { }.
• Repeat until queue is empty:
• Dequeue the first vertex v from the queue.
• ordering += v.
• Decrease the in-degree of all v's neighbors by 1 in the map.
• que ue += {any neighbors whose in-degree is now 0}.
• If all vertices are processed, success.
Otherwise, there is a cycle.
Topological sort example 2
• function topologicalSort():
• map := {each vertex → its in-degree}. C
• que ue := {all vertices with in-degree = 0}. F
• ordering := { }. B E
• Repeat until queue is empty:
• Dequeue the first vertex v from the queue.
D
• ordering += v.
• Decrease the in-degree of all v's
neighbors by 1 in the map. A
• queue += {any neighbors whose in-degree is now 0}.
• function topologicalSort():
• map := {each vertex → its in-degree}. // O(V)
• queue := {all vertices with in-degree = 0}.
• ordering := { }.
• Repeat until queue is empty: // O(V)
• Dequeue the first vertex v from the queue. // O(1)
• ordering += v. // O(1)
• Decrease the in-degree of all v's // O(E ) for all passes
neighbors by 1 in the map.
• queue += {any neighbors whose in-degree is now 0}.