Upload
vuongcong
View
217
Download
1
Embed Size (px)
Citation preview
1
14 September 2010 1
CAD Algorithms
Shortest Path Algorithms
Mohammad Tehranipoor
ECE Department
14 September 2010 2
Shortest Path
� Problem:
� Find the best way of getting from s to t where s and tare vertices in a graph.
� Best: Min (sum of the edge lengths of a path)
� On a weighted graph:
� Shortest path = Min (∑ f(p)), where p Є P (P is the set of
all paths)
� Example:
� Traveling Salesman Problem (TSP)
� Fastest path or shortest path are the same.
� The length of a path can be actual mileage or time to drive it.
2
14 September 2010 3
Assumption
� All edges have lengths (weights) that are positivenumbers.
� It makes the algorithm a lot easier.
� It can be applied to both directed and undirected graph
� There are algorithms that handle negative weights/lengths/costs.
� Bellman-Ford Algorithm
14 September 2010 4
Path from Distance
s x t
Path: d(s,t)
d(s,t) = d(s,x) + d(x,t)
Therefore:
d(s,x) < d(s,t)
Objective: Find the shortest path from s to t
3
14 September 2010 5
Dijkstra’s Algorithm
� Given a connected weighted graph G=(V,E), a weight d:E R+ and a fixed vertex s in V, find the shortest path from s to each vertex v in V.
� Dijkstra’s algorithm works almost the same as Prim’s algorithm. The only difference is that it chooses an edge once at a time instead of vertex to find the shortest path or minimize d(s,x) + length(x,y).
14 September 2010 6
Dijkstra’s Algorithm
1: Let T be a single vertex s
2: While (T has fewer than n vertices) // n: total number of vertices
3: {
4: Find edge (x,y)
5: with x in T and y not in T Minimizing d(s,x)+length(x,y)
7: Add (x,y) to T
8: d(s,y) = d(s,x) + length (x,y)
9: }
4
14 September 2010 7
Example
816
7
11
10
5
17
13
14
6
A
E
CB
D
F
Problem: Find the shortest paths from A to all vertices
14 September 2010 8
Cont.
816
7
11
10
5
17
13
14
6
A
E
CB
D
F
T = {A}
5
14 September 2010 9
Cont.
816
7
11
10
5
17
13
14
6
A
E
CB
D
F
T = {A}
8
16
13
AC 8
AD 13
AE 16
14 September 2010 10
Cont.
816
7
11
10
5
17
13
14
6
A
E
CB
D
F
T = {A, C}
8
16
13
6
14 September 2010 11
Cont.
816
7
11
10
5
17
13
14
6
A
E
CB
D
F
T = {A, C}
8
15
25
13
18AD 13
ACD 19
ACB 18
AE 16
ACE 15
ACF 25
14 September 2010 12
Cont.
8
7
10
5
17
13
14
6
A
E
CB
D
F
T = {A, C, D, E}
8
15
25
13
18
16
11
7
14 September 2010 13
Cont.
8
7
10
5
17
13
14
6
A
E
CB
D
F
T = {A, C, D, E, B}
8
15
20
13
18
16
11
14 September 2010 14
Cont.
8
7
10
5
17
13
14
6
A
E
CB
D
F
T = {A, C, D, E, B, F}
8
15
20
13
18
16
11
AC 8
AD 13
ACE 15
ACB 18
ACEF 20
8
14 September 2010 15
Prim’s Algorithm
{
T = ;
U = {first vertex};
while (U V)
{
let (u, v) be the lowest cost edge
such that u U and v V - U;T = T {(u, v)}
U = U {v}
}
}
14 September 2010 16
Prim’s Algorithm
Problem: Find the shortest paths from A to all vertices
A
E
C
B D
F
6
6
63
4
1
2
5
5
5
U = {}
9
14 September 2010 17
Cont.
A
E
C
B D
F
1
U = {A}
A
E
C
B D
F
6
6
63
4
1
2
5
5
5
14 September 2010 18
Cont.
A
E
C
B D
F
1
U = {A, C}
4
A
E
C
B D
F
6
6
63
4
1
2
5
5
5
10
14 September 2010 19
Cont.
A
E
C
B D
F
4
1
2
U = {A, C, F}
A
E
C
B D
F
6
6
63
4
1
2
5
5
5
14 September 2010 20
Cont.
T = {A, C, F, D}
A
E
C
B D
F
4
1
2
5
A
E
C
B D
F
6
6
63
4
1
2
5
5
5
11
14 September 2010 21
Cont.
T = {A, C, F, D, B}
A
E
C
B D
F
3
4
1
2
5
A
E
C
B D
F
6
6
63
4
1
2
5
5
5
14 September 2010 22
Cont.
T = {A, C, F, D, B, E}
A
E
C
B D
F
6
6
63
4
1
2
5
5
5
This algorithm is more suitable for MST problem.
12
14 September 2010 23
CAD Algorithms
Minimum Spanning Tree Algorithms
Mohammad Tehranipoor
ECE Department
14 September 2010 24
MST
� MST is a well-solved combinatorial optimization problem.
� Definition:
� A tree is a connected graph without cycles.
� Properties of Trees:
� A graph is a tree if and only if there is only and only one path joining any two of its vertices.
� A connected graph is a tree if and only if it has N vertices and N-1 edges.
13
14 September 2010 25
MST (Cont.)
� Definition:
� A subgraph that spans (reaches out to) all vertices of a graph is called a spanning subgraph.
� A subgraph that is a tree and that spans (reaches out to) all vertices of the original graph is called a spanning tree.
� Among all the spanning trees of a weighted and connected graph, the one (possibly more) with the least total weight is called a minimum spanning tree (MST).
14 September 2010 26
Example
� A simple example:� A graph may have many spanning trees.
� This graph has 16 spanning trees.
5
3
1
3
2
4
14
14 September 2010 27
Example: Trees
� 16 spanning trees:
Tree #: 1 2 16
3 1
2
MST
14 September 2010 28
Problem
� Suppose the edges of the graph have weights.� The weights of a tree is sum of weights of its edges.
� Different trees may have different weights.
� Problem:� How to find minimum spanning tree?
� Solutions:� Heuristic
� Greedy
� Exhaustive search
� …
15
14 September 2010 29
Why Minimum Spanning Tree?
� Example:� Phone Network Design:
� One has a business with several offices and wants to lease phone lines to connect them up with each other; the phone company charges differently to connect different pair of cities. How he can find a set of lines that connects all the offices with a minimum total cost.
� Traveling Salesman Problem (TSP):� Another convenient way to solve this problem is to find
the shortest path that visits each point at least once.
� VLSI Problem: Global Routing� MST is used in global routing
� Steiner Tree is used in detailed (channel) routing
14 September 2010 30
MST/TSP
� In general the MST weight is less than the TSP. In MST, we are not limited only to paths.
� What is path:
Path Path Not a path
Not a TSP solution
16
14 September 2010 31
How to Find MST?
� An exhaustive search: � List all spanning trees and find the minimum one. Not
applicable in practice especially in VLSI problems.
� Heuristic Algorithms
� Greedy Algorithms� Prim’s Algorithm
14 September 2010 32
Kruskal’s Algorithm
� It is easy to understand and the best for solving problems by hands.
� Kruskal’s Algorithm:
� Step 1: Sort the edges of graph G in increasing order by weight
� Step 2: Keep a subgraph S of G, initially empty
� Step 3: For each edge e in sorted order
� If the endpoints of e are disconnected in S
� Add e to S
� Step 4: Return S
17
14 September 2010 33
Example
12
3
1
12
2
2
2
S={}
A
B
E
D
C
F
Weight
Edge
1
AB
32222211
EFDFDECEAEADBDAC
14 September 2010 34
Cont.
12
3
1
12
2
2
2
Edges: 1, 1, 1, 2, 2, 2, 2, 2, 3
S={AB}
A
B
E
D
C
F
Weight
Edge
1
AB
32222211
EFDFDECEAEADBDAC
18
14 September 2010 35
Cont.
12
3
1
12
2
2
2A
B
E
D
C
F
Edges: 1, 1, 1, 2, 2, 2, 2, 2, 3
S={AB, BD}
Weight
Edge
1
AB
32222211
EFDFDECEAEADBDAC
14 September 2010 36
Cont.
12
3
1
12
2
2
2A
B
E
D
C
F
Edges: 1, 1, 1, 2, 2, 2, 2, 2, 3
S={AB, BD, AC}
Weight
Edge
1
AB
32222211
EFDFDECEAEADBDAC
19
14 September 2010 37
Cont.
12
3
1
12
2
2
2A
B
E
D
C
F
Edges: 1, 1, 1, 2, 2, 2, 2, 2, 3
S={AB, BD, AC, AE}
Weight
Edge
1
AB
32222211
EFDFDECEAEADBDAC
14 September 2010 38
Cont.
12
3
1
12
2
2
2A
B
E
D
C
F
Edges: 1, 1, 1, 2, 2, 2, 2, 2, 3
S={AB, BD, AC, AE}
Weight
Edge
1
AB
32222211
EFDFDECEAEADBDAC
20
14 September 2010 39
Cont.
12
3
1
12
2
2
2A
B
E
D
C
F
Edges: 1, 1, 1, 2, 2, 2, 2, 2, 3
S={AB, BD, AC, AE}
Weight
Edge
1
AB
32222211
EFDFDECEAEADBDAC
14 September 2010 40
Cont.
12
3
1
12
2
2
2
Total Cost = 7
A
B
E
D
C
F
Edges: 1, 1, 1, 2, 2, 2, 2, 2, 3
S={AB, BD, AC, AE, DF}
Weight
Edge
1
AB
32222211
EFDFDECEAEADBDAC
21
14 September 2010 41
Example
� A graph may have more than one MST.
� This algorithm is known as a greedy algorithm because it chooses the cheapest edge at each step and adds to S.
12
3
1
12
2
2
2
Total Cost = 7
A
B
E
D
C
F
12
3
1
12
2
2
2
Total Cost = 7
A
B
E
D
C
F
14 September 2010 42
Prim’s Algorithm
� Step 1: Pick any vertex as a starting vertex. Call it s.
� Step 2: Find the nearest neighbor of s (call it p1). Choose sp1 and p1 such that sp1 is the cheapest edge in the graph that does not close the selected edges. Add sp1 to S.
� Step 3: Return subgraph S.
22
14 September 2010 43
Example
1
2
3 6 7 8
9
5 12
11104
9
4
3
4
3
614
3
3
10
6
37
Prim’s Algorithm:
14 September 2010 44
Cont.
1
2
3 6 7 8
9
5 12
11104
9
4
3
4
3
614
3
3
10
6
37
23
14 September 2010 45
Cont.
1
2
3 6 7 8
9
5 12
11104
9
4
3
4
3
614
3
3
10
6
37
14 September 2010 46
Cont.
1
2
3 6 7 8
9
5 12
11104
9
4
3
4
3
614
3
3
10
6
37
24
14 September 2010 47
Cont.
1
2
3 6 7 8
9
5 12
11104
9
4
3
4
3
614
3
3
10
6
37
14 September 2010 48
Cont.
1
2
3 6 7 8
9
5 12
11104
9
4
3
4
3
614
3
3
10
6
37
25
14 September 2010 49
Cont.
1
2
3 6 7 8
9
5 12
11104
9
4
3
4
3
614
3
3
10
6
37
14 September 2010 50
Cont.
1
2
3 6 7 8
9
5 12
11104
9
4
3
4
3
614
3
3
10
6
37
26
14 September 2010 51
Cont.
1
2
3 6 7 8
9
5 12
11104
9
4
3
4
3
614
3
3
10
6
37
14 September 2010 52
Cont.
1
2
3 6 7 8
9
5 12
11104
9
4
3
4
3
614
3
3
10
6
37
27
14 September 2010 53
Cont.
1
2
3 6 7 8
9
5 12
11104
9
4
3
4
3
614
3
3
10
6
37
14 September 2010 54
Cont.
1
2
3 6 7 8
9
5 12
11104
9
4
3
4
3
614
3
3
10
6
37
28
14 September 2010 55
Cont.
1
2
3 6 7 8
9
5 12
11104
9
4
3
4
3
614
3
3
10
6
37
14 September 2010 56
Cont.
1
2
3 6 7 8
9
5 12
11104
9
4
3
4
3
614
3
3
10
6
37
29
14 September 2010 57
More Algorithms
� Boruvka’s Algorithm:� The idea is to do steps like Prim’s algorithm in parallel all over
the graph at the same time.
� Hybrid Algorithm:� Combine two of the classical algorithms and do better than
either one alone.