0% found this document useful (0 votes)
6 views

Greedy Algorithm - Part 2

The document discusses greedy algorithms for finding minimum spanning trees and shortest paths in graphs. It explains Kruskal's and Prim's algorithms for finding minimum spanning trees, and Dijkstra's algorithm for finding shortest paths. Examples are provided and time complexities analyzed.

Uploaded by

jayden goh1000
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Greedy Algorithm - Part 2

The document discusses greedy algorithms for finding minimum spanning trees and shortest paths in graphs. It explains Kruskal's and Prim's algorithms for finding minimum spanning trees, and Dijkstra's algorithm for finding shortest paths. Examples are provided and time complexities analyzed.

Uploaded by

jayden goh1000
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

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

– Repeatedly (until all nodes are known),


■ Find an unknown node containing the smallest distance
■ Mark the new node as known
■ For each node adjacent to the new node, examine its neighbors to see
whether their estimated distance can be reduced(distance to known
node plus cost of out-edge )
– If so, also reset the predecessor of the new node
Dijkstra’s shortest-path algorithm

■ while Q is not empty do


– u := extract-min(Q)
– for each neighbor v of u do (relaxation)
■ if d[u] + w(u,v) < d[v] then
– d[v] := d[u] + w(u,v)
– decrease-key(Q,v,d[v])
– parent(v) := u
Dijkstra’s shortest-path algorithm :
Simple example

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

Fail to find the shortest path A->B-> C (-5)


Note : Dijkstra’s algorithm might give incorrect shortest path for negative weight
Use dynamic programming strategy (Bell-man ford) for the negative weight

You might also like