Greedy Algorithm - Part 2
Greedy Algorithm - Part 2
Learning Outcome
Successful students should be able to:
▪ Explain Spanning Tree
▪ find the Minimum Spanning Tree using Kruskal’s & Prim’s algorithms
▪ Find the shortest path using Djistral’s algorithms
What is Spanning Tree
■ Given an undirected and connected graph G=(V,E), a spanning tree of the
graph G is a tree that spans G (that is, it includes every vertex of G) and is
a subgraph of G (every edge in the tree belongs to G)
– |V| = n
– |E| = n-1
Minimum Spanning Tree (MST)
Minimum Spanning Tree (MST)
■ In a weighted graph, a minimum spanning tree is a spanning
tree that has minimum weight than all other spanning trees of
the same graph.
■ In real-world situations, this weight can be measured as
distance, congestion, traffic load or any arbitrary value denoted
to the edges.
■ Applications of MST (Refer to link on the Blackboard):
– Civil Network Planning
– Computer Network Routing Protocol
– Clustering and many more
■ Two common greedy algorithms to find MST:
– Kruskal’s Algorithms
– Prim’s Algorithms
Kruskal’s algorithm
1. Sort all the edges in non-decreasing order of their weight.
2. Pick the smallest edge. Check if it forms a cycle with the spanning
tree formed so far. If cycle is not formed, include this edge. Else,
discard it.
3. Repeat step#2 until there are (V-1) edges in the spanning tree.
Kruskal’s algorithm
Kruskal’s algorithm
Kruskal’s algorithm
Kruskal’s algorithm
Example 2: Question
16
5 4 11
12
7
3 14
6 9 2
10 8
15
17
13 18
Example 2: Answer
16
5 4 11
12
7
3 14
6 9 2
10 8
15
17
13 18
Kruskal’s algorithm
■ the time to sort the edges in line 4 is O(E lg E)
Prim’s Algorithm
■ Start with any vertex(say A) of the graph G(V, E), to build a tree T.
■ Add the least cost edge to the tree T provided one of the endpoints
of that edge must be in T.
■ Add the edges until all the vertices are covered. Same here also,
the cycle should not be formed on adding an edge.
Prim’s Algorithm
Prim’s Algorithm Example1
Example1 : Answer
Prim’s Algorithm Example 2
Prim’s Algorithms Example 2: Answer
Prim’s Algorithm Example 2
Prim’s Algorithm
– total time is O(E log V)
Shortest Path
■ The shortest path problem is about finding a path between 2 vertices
in a graph such that the total sum of the edges weights is minimum.
■ Two common algorithms:
– Dijkstra’s shortest-path algorithm
– Bellman-Ford algorithm
(Dynamic programming approach)
Dijkstra’s shortest-path algorithm
■ Dijkstra’s algorithm finds the shortest paths from a given node to all other
nodes in a graph
– Initially,
■ Mark the given node as known (path length is zero)
■ For each out-edge, set the distance in each neighboring node equal to
the cost (length) of the out-edge, and set its predecessor to the initially
given node
B
1 5
A d
3 C 1
Dijkstra’s shortest-path algorithm : Example 1
Dijkstra’s shortest-path algorithm : Answer
Dijkstra’s shortest-path algorithm : Example 2
(Start from Vertex C)
Dijkstra’s shortest-path algorithm :
Example 2 Answer
Dijkstra’s shortest-path algorithm : Example 3
With negative weight