Graph Algorithms-Protected - Unlocked
Graph Algorithms-Protected - Unlocked
Graph Representation
Bachelor of Technology
CSE (AIML)/ICT
• Graph
Adjacency List
Space – 𝜃(𝑉 + 𝐸)
• A non-linear data structure denoted by 𝐺 𝑉, 𝐸 .
• 𝑉 - set of vertices/nodes
Analysis and Design of Algorithms • 𝐸 – set of edges
Module 2
• Representing/storing graphs in memory.
Elementary Graph Algorithms
Adjacency
Adjacency List
Matrix Graph
Dr. Anuj Kr. Singh Representation
Associate Professor Two Ways Adjacency Matrix
Sparse Graphs Dense Graphs
Computer Science & Engineering |𝐸| ≪ |𝑉| |𝐸| ≅ |𝑉| Space – 𝜃(𝑉 )
• The adjacency-list representation of a graph 𝐺 𝑉, 𝐸 consists of an array 𝐴𝑑𝑗 of |𝑉| lists, one for Example 1 (UDG) Example 2 (DG)
each vertex in 𝑉 .
• For each 𝑢 ∈ 𝑉 , the adjacency list 𝐴𝑑𝑗(𝑢) contains all the vertices such that there is an edge
(𝑢, 𝑣) ∈ 𝑉.
• 𝑨𝒅𝒋 is actually an array of |𝑽| lists corresponding to each vertex in the graph.
12-09-2023 Dr. Anuj Kr. Singh, Associate Professor 3 12-09-2023 Dr. Anuj Kr. Singh, Associate Professor 4
12-09-2023
• For the adjacency-matrix representation of a graph 𝐺 𝑉, 𝐸 , we assume that the vertices are Example 1 (UDG) Example 2 (DG)
numbered 1, 2, … … . , |𝑉| in some arbitrary manner.
12-09-2023 Dr. Anuj Kr. Singh, Associate Professor 5 12-09-2023 Dr. Anuj Kr. Singh, Associate Professor 6
• Both BFS and DFS runs in θ(𝑉 + 𝐸) time. • It produces a “Breadth-First Tree (BFT)” with root s that contains all reachable vertices.
• For any vertex reachable from s, the simple path in the breadth-first tree from s to corresponds to
• BFS uses QUEUE a “shortest path” from s to in G, that is, a path containing the smallest number of edges.
• DFS uses STACK.
Graph Searching • The algorithm works on both directed and undirected graphs.
Two Algorithms DFS
(Depth First Search)
Output – A Forest (DFF)
12-09-2023 Dr. Anuj Kr. Singh, Associate Professor 7 12-09-2023 Dr. Anuj Kr. Singh, Associate Professor 8
12-09-2023
• The strategy followed by depth-first search is, as its name implies, to search “deeper” in the graph
Example whenever possible.
• Depth-first search explores edges out of the most recently discovered vertex that still has
unexplored edges leaving it.
• Once all of the edges have been explored, the search “backtracks” to explore edges leaving the
vertex from which it was discovered.
• This process continues until we have discovered all the vertices that are reachable from the
original source vertex.
12-09-2023 Dr. Anuj Kr. Singh, Associate Professor 11 12-09-2023 Dr. Anuj Kr. Singh, Associate Professor 12
12-09-2023
DFS-VISIT(u)
1
1 color[u] ← GRAY ▹White vertex u has just been discovered.
2 me ← me +1 2
3 d[u] time // record the discovery time 3
4 for each v ∈ Adj[u] ▹Explore edge(u, v). White
5 do if color[v] = WHITE (Undiscovered)
Gray
6 then π[v] ← u // set the parent value
(Discovered)
7 DFS-VISIT(v) // recursive call Black
8 color[u] BLACK ▹ Blacken u; it is finished. (Finished)
9 f [u] ▹ me ← me +1
12-09-2023 Dr. Anuj Kr. Singh, Associate Professor 13 12-09-2023 Dr. Anuj Kr. Singh, Associate Professor 14
• A Topological Sort of a DAG G(V,E) is a linear ordering of all its vertices such that if G contains an
Two edge (u,v) then u appears before in the ordering.
Applications
• If the graph contains a cycle, then no linear ordering is possible.
12-09-2023 Dr. Anuj Kr. Singh, Associate Professor 15 12-09-2023 Dr. Anuj Kr. Singh, Associate Professor 16
12-09-2023
Linearly
Ordered List
12-09-2023 Dr. Anuj Kr. Singh, Associate Professor 17 12-09-2023 Dr. Anuj Kr. Singh, Associate Professor 18
SCC
GT