CAD Algorithms Shortest Path

CAD Algorithms
Shortest Path Algorithms
Mohammad Tehranipoor
ECE Department
14 September 2010
1
Shortest Path
Problem:
Find the best way of getting from s to t where s and t
are vertices in a graph.
Best: Min (sum of the edge lengths of a path)
On a weighted graph:
f(p)),
where p Є P (P is the set of
Example:
Shortest path = Min (∑
all paths)
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.
14 September 2010
2
1
Assumption
All edges have lengths (weights) that are positive
numbers.
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
3
Path from Distance
Objective: Find the shortest path from s to t
s
x
t
Path: d(s,t)
d(s,t) = d(s,x) + d(x,t)
Therefore:
d(s,x) < d(s,t)
14 September 2010
4
2
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
5
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: }
14 September 2010
6
3
Example
Problem: Find the shortest paths
from A to all vertices
A
13
B
8
16
C
10
7
11
17
6
D
E
5
14
F
14 September 2010
7
Cont.
T = {A}
A
13
B
8
16
C
10
7
11
17
6
D
14
14 September 2010
E
5
F
8
4
Cont.
T = {A}
A
13
B
AC
8
AD
13
AE
16
8
16
8
C
10
7
11
17
E
16
13
6
D
14
5
F
14 September 2010
9
Cont.
T = {A, C}
A
13
B
8
16
8
C
10
7
11
17
E
16
13
6
D
14
14 September 2010
5
F
10
5
Cont.
T = {A, C}
A
13
AD
13
ACD
19
ACB
18
AE
16
ACE
15
18
B
8
16
8
C
10
7
11
17
15
13
ACF
E
6
D
14
5
F
25
25
14 September 2010
11
Cont.
T = {A, C, D, E}
A
13
18
B
8
16
8
C
10
7
11
17
13
6
D
14
E
15
5
F
25
14 September 2010
12
6
Cont.
T = {A, C, D, E, B}
A
13
18
B
8
16
8
C
10
7
11
17
13
E
6
D
14
15
5
F
20
14 September 2010
13
Cont.
T = {A, C, D, E, B, F}
A
13
18
AC
8
AD
13
ACE
15
ACB
18
ACEF
20
B
8
16
8
C
10
7
11
17
13
6
D
14
E
15
5
F
20
14 September 2010
14
7
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
15
Prim’s Algorithm
Problem: Find the shortest paths
from A to all vertices
U = {}
A
5
6
B
1
D
5
5
C
3
E
6
2
4
6
F
14 September 2010
16
8
Cont.
U = {A}
A
A
5
6
1
B
D
5
5
1
B
D
C
C
3
6
2
4
E
E
6
F
F
14 September 2010
17
Cont.
U = {A, C}
A
A
5
6
1
B
D
1
B
D
5
5
C
C
3
6
E
4
2
4
E
F
6
F
14 September 2010
18
9
Cont.
U = {A, C, F}
A
A
5
6
1
B
D
D
5
5
1
B
C
C
3
6
E
2
4
2
4
E
6
F
F
14 September 2010
19
Cont.
T = {A, C, F, D}
A
A
5
6
1
B
D
5
5
B
6
E
2
4
2
4
E
6
F
14 September 2010
D
5
C
C
3
1
F
20
10
Cont.
T = {A, C, F, D, B}
A
A
5
6
1
B
5
5
1
B
D
D
5
C
C
3
6
E
3
2
4
2
4
E
6
F
F
14 September 2010
21
Cont.
T = {A, C, F, D, B, E}
A
5
6
B
1
D
5
5
C
3
E
6
2
4
6
F
This algorithm is more suitable for MST problem.
14 September 2010
22
11
CAD Algorithms
Minimum Spanning Tree Algorithms
Mohammad Tehranipoor
ECE Department
14 September 2010
23
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.
14 September 2010
24
12
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
25
Example
A simple example:
A graph may have many spanning trees.
2
1
5
3
3
4
This graph has 16 spanning trees.
14 September 2010
26
13
Example: Trees
16 spanning trees:
Tree #: 1
2
16
2
MST
3
1
14 September 2010
27
Problem
Suppose the edges of the graph have weights.
Problem:
The weights of a tree is sum of weights of its edges.
Different trees may have different weights.
How to find minimum spanning tree?
Solutions:
Heuristic
Greedy
Exhaustive search
…
14 September 2010
28
14
Why Minimum Spanning Tree?
Example:
Phone Network Design:
Traveling Salesman Problem (TSP):
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.
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
29
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
14 September 2010
30
15
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
31
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
14 September 2010
32
16
Example
Edge
AB
AC
BD
AD
AE
CE
DE
DF
EF
Weight
1
1
1
2
2
2
2
2
3
S={}
B
1
D
2
1
2
2
A
1
2
F
3
2
C
E
14 September 2010
33
Cont.
Edge
AB
AC
BD
AD
AE
CE
DE
DF
EF
Weight
1
1
1
2
2
2
2
2
3
Edges: 1, 1, 1, 2, 2, 2, 2, 2, 3
S={AB}
B
1
D
2
1
2
2
A
F
3
1
C
14 September 2010
2
2
E
34
17
Cont.
Edge
AB
AC
BD
AD
AE
CE
DE
DF
EF
Weight
1
1
1
2
2
2
2
2
3
Edges: 1, 1, 1, 2, 2, 2, 2, 2, 3
S={AB, BD}
B
D
1
2
1
2
2
A
F
2
3
1
C
2
E
14 September 2010
35
Cont.
Edge
AB
AC
BD
AD
AE
CE
DE
DF
EF
Weight
1
1
1
2
2
2
2
2
3
Edges: 1, 1, 1, 2, 2, 2, 2, 2, 3
S={AB, BD, AC}
B
1
D
2
1
2
2
A
F
3
1
C
14 September 2010
2
2
E
36
18
Cont.
Edge
AB
AC
BD AD AE
CE
DE
DF
EF
Weight
1
1
1
2
2
2
3
2
2
Edges: 1, 1, 1, 2, 2, 2, 2, 2, 3
S={AB, BD, AC, AE}
B
D
1
2
1
2
2
A
2
F
3
1
2
C
E
14 September 2010
37
Cont.
Edge
AB
AC
BD
AD
AE
CE
DE
DF
EF
Weight
1
1
1
2
2
2
2
2
3
Edges: 1, 1, 1, 2, 2, 2, 2, 2, 3
S={AB, BD, AC, AE}
B
1
D
2
1
2
2
A
1
14 September 2010
C
2
2
F
3
E
38
19
Cont.
Edge
AB
AC
BD
AD
AE
CE
DE
DF
EF
Weight
1
1
1
2
2
2
2
2
3
Edges: 1, 1, 1, 2, 2, 2, 2, 2, 3
S={AB, BD, AC, AE}
B
D
1
2
1
2
2
A
F
2
3
1
C
2
E
14 September 2010
39
Cont.
Edge
AB
AC
BD
AD
AE
CE
DE
DF
EF
Weight
1
1
1
2
2
2
2
2
3
Edges: 1, 1, 1, 2, 2, 2, 2, 2, 3
S={AB, BD, AC, AE, DF}
B
1
D
2
1
2
2
A
2
F
3
1
C
2
E
Total Cost = 7
14 September 2010
40
20
Example
A graph may have more than one MST.
B
1
D
B
2
1
2
2
A
2
F
C
2
E
Total Cost = 7
D
2
2
2
A
3
1
1
1
2
F
3
1
C
2
E
Total Cost = 7
This algorithm is known as a greedy algorithm because it
chooses the cheapest edge at each step and adds to S.
14 September 2010
41
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.
14 September 2010
42
21
Example
Prim’s Algorithm:
4
3
1
6
10
6
7
8
7
6
2
3
3
5
12
3
3
9
4
4
10
11
4
3
1
9
14 September 2010
43
Cont.
4
3
1
6
10
6
7
8
7
6
2
3
3
5
12
3
3
9
4
4
10
11
4
1
14 September 2010
3
9
44
22
Cont.
4
3
1
6
10
6
7
8
7
6
2
3
3
5
12
3
3
9
4
4
10
11
4
3
1
9
14 September 2010
45
Cont.
4
3
1
6
10
6
7
8
7
6
2
3
3
5
12
3
3
9
4
4
10
11
4
1
14 September 2010
3
9
46
23
Cont.
4
3
1
6
10
6
7
8
7
6
2
3
3
5
12
3
3
9
4
4
10
11
4
3
1
9
14 September 2010
47
Cont.
4
3
1
6
10
6
7
8
7
6
2
3
3
5
12
3
3
9
4
4
10
11
4
1
14 September 2010
3
9
48
24
Cont.
4
3
1
6
10
6
7
8
7
6
2
3
3
5
12
3
3
9
4
4
10
11
4
3
1
9
14 September 2010
49
Cont.
4
3
1
6
10
6
7
8
7
6
2
3
3
5
12
3
3
9
4
4
10
11
4
1
14 September 2010
3
9
50
25
Cont.
4
3
1
6
10
6
7
8
7
6
2
3
3
5
12
3
3
9
4
4
10
11
4
3
1
9
14 September 2010
51
Cont.
4
3
1
6
10
6
7
8
7
6
2
3
3
5
12
3
3
9
4
4
10
11
4
1
14 September 2010
3
9
52
26
Cont.
4
3
1
6
10
6
7
8
7
6
2
3
3
5
12
3
3
9
4
4
10
11
4
3
1
9
14 September 2010
53
Cont.
4
3
1
6
10
6
7
8
7
6
2
3
3
5
12
3
3
9
4
4
10
11
4
1
14 September 2010
3
9
54
27
Cont.
4
3
1
6
10
6
7
8
7
6
2
3
3
5
12
3
3
9
4
4
10
11
4
3
1
9
14 September 2010
55
Cont.
4
3
1
6
10
6
7
8
7
6
2
3
3
5
12
3
3
9
4
4
10
11
4
1
14 September 2010
3
9
56
28
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.
14 September 2010
57
29