Btech Sem 2 Unit 5
Btech Sem 2 Unit 5
Dijkstra’s Algorithm – Minimum Spanning Tree – Prim’s Algorithm – Applications of Depth- First
Search – Undirected Graphs – Bi-connectivity – Introduction to NP
Topological Sorting
Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of
vertices such that for every directed edge u-v, vertex u comes before v in the
ordering.
Note: Topological Sorting for a graph is not possible if the graph is not a
DAG.
Input: V = 6, edges = [[2, 3], [3, 1], [4, 0], [4, 1], [5, 0], [5, 2]]
Example:
Output: 5 4 2 3 1 0
Explanation: The first vertex in topological sorting is always a vertex with an
in-degree of 0 (a vertex with no incoming edges). A topological sorting of
the following graph is "5 4 2 3 1 0". There can be more than one topological
sorting for a graph. Another topological sorting of the following graph is "4 5
2 3 1 0".
2. Concept
3. Importance / Applications
5. Algorithms Used
● DFS-Based Algorithm
● Example: A → B → C → A (Cycle)
11. Summary
nput: V = 8, E = 10, S = 0, D = 7, edges[][] = {{0, 1}, {1, 2}, {0, 3}, {3, 4}, {4, 7},
{3, 7}, {6, 7}, {4, 5}, {4, 6}, {5, 6}}
Output: 0 3 7
Explanation: The shortest path is 0 -> 3 -> 7.
Input: V = 8, E = 10, S = 2, D = 6, edges[][] = {{0, 1}, {1, 2}, {0, 3}, {3, 4}, {4, 7},
{3, 7}, {6, 7}, {4, 5}, {4, 6}, {5, 6}}
Output: 2 1 0 3 4 6
Explanation: The shortest path is 2 -> 1 -> 0 -> 3 - > 4 -> 6.
Approach:
Dijkstra's Algorithm
Now we get the length of the path from source to any other vertex from
array dist[], and for printing the path from source to any vertex we can use
array par[].
Note: The given graph does not contain any negative edge.
Input: src = 0, V = 5, edges[][] = [[0, 1, 4], [0, 2, 8], [1, 4, 6], [2, 3, 2], [3, 4,
10]]
Output: 0 4 8 10 10
Explanation: Shortest Paths:
0 to 1 = 4. 0 → 1
0 to 2 = 8. 0 → 2
0 to 3 = 10. 0 → 2 → 3
0 to 4 = 10. 0 → 1 → 4
🔍 Depth-First Search (DFS)
Depth-First Search (DFS) is a graph traversal algorithm that starts from a
selected node (called the source or root) and explores as far as possible
along each branch before backtracking.
🎯 Graph Example:
🔁 DFS Traversal (Starting from A):
1. Visit A → Mark A visited
8. Done!
✅ DFS Order:
A→B→D→C→E→F
🌐 Applications of DFS
DFS is a powerful algorithm used in many important graph-based problems.
2. 🔁 Topological Sorting
● Applicable only in Directed Acyclic Graphs (DAGs).
● DFS is used to sort nodes such that for every directed edge u → v, u
comes before v.
● Has no cycles.
2. Pick the smallest edge. If it doesn’t form a cycle, include it.
🪜 Steps:
Sorted edges:
● B–C (1)
● A–B (2)
● A–D (3)
● C–E (4)
● B–E (5)
● A–C (6)
● D–E (7)
2. At each step, pick the smallest edge that connects a visited node to an
unvisited node.
● At each step, adding the cheapest edge from the visited set to an
unvisited vertex.
Approach Greedy
🔁 Step-by-Step Procedure
1. Initialize an empty MST.
📘 1. Undirected Graphs
An undirected graph is a graph where edges have no direction. That means
if there is an edge between nodes u and v, you can travel from u to v and
also from v to u.
📌 Key Terminology:
● Vertex (Node): A point in the graph.
✅ Properties:
● Undirected: Edge (A–B) is same as (B–A)
● It is connected
● There are no articulation points (i.e., no single vertex whose removal
disconnects the graph)
● Articulation points
✅ Algorithm (DFS-based):
1. Do a DFS traversal.
○ Discovery time
💡 3. Introduction to NP-Completeness
⚙️ What is NP-Completeness?
● P = Class of problems that can be solved in polynomial time
○ In NP