Title: Greedy 2
1Greedy 2
- Jose Rolim
- University of Geneva
2Examples Greedy
- Minimum Spanning Trees
- Shortest Paths Dijkstra
3Definition MST
- Given a connected graph G (V, E), with weight
function w E --gt R - Min-weight connected subgraph
- Spanning tree T
- A tree that includes all nodes from V
- T (V, E), where E ? E
- Weight of T W( T ) ? w(e)
- Minimum spanning tree (MST)
- A tree with minimum weight among all spanning
trees
4Example
5MST
- MST for given G may not be unique
- Since MST is a spanning tree
- edges V - 1
- If the graph is unweighted
- All spanning trees have same weight
6Kruskals algorithm (Greedy)
- Start with A empty, and each vertex being its own
connected component - Repeatedly merge two components by connecting
them with a light edge crossing them - Two issues
- Maintain sets of components
- Choose light edges
7Pseudo-code
8The Greedy Algorithm in Action
4
2
6
1
5
3
7
9The Greedy Algorithm in Action
10
8
10
8
2
4
6
4
2
6
35
35
15
15
1
1
25
25
20
20
30
30
17
17
21
21
11
40
40
3
5
7
5
3
7
15
15
11
10The Greedy Algorithm in Action
10
8
10
8
2
4
6
4
2
6
35
35
15
15
1
1
25
25
20
20
30
30
17
17
21
21
11
40
40
3
5
7
5
3
7
15
15
11
11The Greedy Algorithm in Action
10
8
10
8
2
4
6
4
2
6
35
35
15
15
1
1
25
25
20
20
30
30
17
17
21
21
11
40
40
3
5
7
5
3
7
15
15
11
12The Greedy Algorithm in Action
10
8
10
8
2
4
6
4
2
6
35
35
15
15
1
1
25
25
20
20
30
30
17
17
21
21
11
40
40
3
5
7
5
3
7
15
15
11
13The Greedy Algorithm in Action
10
8
10
8
2
4
6
4
2
6
35
35
15
15
1
1
25
25
20
20
30
30
17
17
21
21
11
40
40
3
5
7
5
3
7
15
15
11
14The Greedy Algorithm in Action
10
8
10
8
2
4
6
4
2
6
35
35
15
15
1
1
25
25
20
20
30
30
17
17
21
21
11
40
40
3
5
7
5
3
7
15
15
11
15The Greedy Algorithm in Action
10
8
10
8
2
4
6
4
2
6
35
35
15
15
1
1
25
25
20
20
30
30
17
17
21
21
11
40
40
3
5
7
5
3
7
15
15
11
16The Greedy Algorithm in Action
10
8
10
8
2
4
6
4
2
6
35
35
15
15
1
1
25
25
20
20
30
30
17
17
21
21
11
40
40
3
5
7
5
3
7
15
15
11
17The Greedy Algorithm in Action
10
8
10
8
2
4
6
4
2
6
35
35
15
15
1
1
25
25
20
20
30
30
17
17
21
21
11
40
40
3
5
7
5
3
7
15
15
11
18The Greedy Algorithm in Action
10
8
10
8
2
4
6
4
2
6
35
35
15
15
1
1
25
25
20
20
30
30
17
17
21
21
11
40
40
3
5
7
5
3
7
15
15
11
19The Greedy Algorithm in Action
10
8
10
8
2
4
6
4
2
6
35
35
15
15
1
1
25
25
20
20
30
30
17
17
21
21
11
40
40
3
5
7
5
3
7
15
15
11
20The Greedy Algorithm in Action
10
8
10
8
2
4
6
4
2
6
35
35
15
15
1
1
25
25
20
20
30
30
17
17
21
21
11
40
40
3
5
7
5
3
7
15
15
11
21The Greedy Algorithm in Action
Node 1 2 3 4 5 6
7 First 1 2 3 4 5
4 7
10
8
10
8
2
4
6
6
4
2
6
35
35
15
15
1
1
25
25
20
20
30
30
17
17
21
21
11
40
40
3
5
7
5
3
7
15
15
11
21
22The Greedy Algorithm in Action
Node 1 2 3 4 5 6
7 First 1 4 3 4 5
4 7
10
8
10
8
2
4
6
2
4
2
6
35
35
15
15
1
1
25
25
20
20
30
30
17
17
21
21
11
40
40
3
5
7
5
3
7
15
15
11
22
23The Greedy Algorithm in Action
Node 1 2 3 4 5 6
7 First 1 4 3 4 5
4 5
10
8
10
8
2
4
6
4
2
6
35
35
15
15
1
1
25
25
20
20
30
30
17
17
21
21
11
40
40
3
5
7
7
5
3
7
15
15
11
23
24The Greedy Algorithm in Action
Node 1 2 3 4 5 6
7 First 1 4 5 4 5
4 5
10
8
10
8
2
4
6
4
2
6
35
35
15
15
1
1
25
25
20
20
30
30
17
17
21
21
11
40
40
3
5
7
7
3
5
3
7
15
15
11
24
25The Greedy Algorithm in Action
Node 1 2 3 4 5 6
7 First 1 4 4 4 4
4 4
10
8
10
8
2
4
6
4
2
6
35
35
15
15
1
1
25
25
20
20
30
30
17
17
21
21
11
40
40
3
5
7
5
7
3
5
3
7
15
15
11
25
26The Greedy Algorithm in Action
Node 1 2 3 4 5 6
7 First 4 4 4 4 4
4 4
10
8
10
8
2
4
6
4
2
6
35
35
15
15
1
1
1
25
25
20
20
30
30
17
17
21
21
11
40
40
3
5
7
3
5
3
7
15
15
11
26
27Complexity
- Time complexity
- make-set, find-set and union operations O(V
E) - O( (V E) ? (V E))
- Sorting
- O(E log E) O(E log V)
- Total
- O(E log V)
28Prims algorithm (also Greedy)
- Start with an arbitrary node from V
- Instead of maintaining a forest, grow a MST
- At any time, maintain a MST for V ? V
- At any moment, find a light edge connecting V
with (V-V) - I.e., the edge with smallest weight connecting
some vertex in V with some vertex in V-V !
29Issues
- Again two issues
- Maintain the tree already build at any moment
- Easy simply a tree rooted at r the starting
node - Find the next light edge efficiently
- For v ? V - V, define key(v) the min distance
between v and some node from V - At any moment, find the node with min key.
30Pseudo-code
31Prims Algorithm in Action
10
8
4
4
6
2
2
6
35
15
1
1
25
20
30
17
21
11
40
5
7
3
5
3
7
15
The minimum cost arc from yellow nodes to green
nodes can be found by placing arc values in a
priority queue.
32Prims Algorithm in Action
8
10
4
4
6
2
2
2
6
35
35
15
1
25
1
30
20
17
21
11
40
5
7
3
5
3
7
15
33Prims Algorithm in Action
8
10
10
10
4
2
2
4
6
4
2
6
35
35
15
1
25
25
1
30
17
20
21
11
40
5
7
3
5
3
7
15
34Prims Algorithm in Action
8
8
8
10
10
10
4
2
2
4
6
6
4
2
6
35
35
15
1
25
25
1
17
30
30
20
20
21
21
11
40
5
7
3
5
3
7
15
35Prims Algorithm in Action
8
8
8
10
10
10
4
2
2
4
6
6
4
2
6
35
35
15
15
15
1
25
25
1
17
30
30
17
20
20
21
21
11
40
5
7
3
5
5
3
7
15
36Prims Algorithm in Action
8
8
8
10
10
10
4
2
2
4
6
6
4
2
6
35
35
15
15
15
1
25
25
1
17
30
30
17
20
20
21
21
11
40
5
7
3
5
5
3
7
15
37Prims Algorithm in Action
8
8
8
10
10
10
4
2
2
4
6
6
4
2
6
35
35
15
15
15
1
25
25
1
17
30
30
17
20
20
21
21
11
11
11
40
5
3
7
5
7
5
3
7
15
15
38Prims Algorithm in Action
8
8
8
10
10
10
4
2
2
4
6
6
4
2
6
35
35
15
15
15
1
25
25
1
17
30
30
17
20
20
21
21
11
11
11
40
5
7
7
3
5
3
5
3
7
15
15
15
39Prims Algorithm in Action
8
8
8
10
10
10
4
2
2
4
6
6
4
2
6
35
35
15
15
15
1
25
25
1
17
30
30
17
20
20
21
21
11
11
11
40
5
7
7
3
5
3
5
3
7
15
15
15
40Prims Algorithm in Action
8
8
8
10
10
10
4
2
2
4
6
6
4
2
6
35
35
15
15
15
1
25
25
1
17
30
30
17
20
20
21
21
11
11
11
40
5
7
7
3
5
3
5
3
7
15
15
15
41Complexity
- Time complexity
- insert
- O(V)
- Decrease-Key
- O( E)
- Extract-Min
- O( V )
- Using heap for priority queue
- Each operation is O ( log V )
- Total time complexity O (E log V)
42 Single-Source Shortest Paths
- Problem Definition
- Shortest paths
- Dijkstras algorithm (can be viewed as a greedy
algorithm)
43Problem Definition
- Real problem A motorist wishes to find the
shortest possible route from Chicago to
Boston.Given a road map of the United States on
which the distance between each pair of adjacent
intersections is marked, how can we determine
this shortest route? - Formal definition Given a graph G(V, E, W),
where each edge has a weight, find a shortest
path from s to v for some interesting vertices s
and v. - ssource
- vdestination.
44Shortest path
- The weight of path pltv0,v1,,vk gt is the sum of
the weights of its constituent edges
The cost of the shortest path from s to v is
denoted as ?(s, v).
45Representing shortest paths
- we maintain for each vertex v?V , a predecessor
- v that is the vertex in the
shortest path - right before v.
- With the values of , a backtracking process
can give the shortest path. (We will discuss that
after the algorithm is given)
46Dijkstras Algorithm
- Dijkstras algorithm assumes that w(e)?0 for each
e in the graph. - maintain a set S of vertices such that
- Every vertex v ?S, dv?(s, v), i.e., the
shortest-path from s to v has been found. (Intial
values Sempty, ds0 and dv?) - (a) select the vertex u?V-S such that
- dumin dxx ?V-S. Set
SS?u - (b) for each node v adjacent to u do
RELAX(u, v, w). - Repeat step (a) and (b) until SV.
47Implementation
- a priority queue Q stores vertices in V-S, keyed
by their d values. - the graph G is represented by adjacency lists.
48u
v
s
y
x
(a)
49u
v
1
10/s
8
10
9
0
s
3
4
6
2
7
5
5/s
8
2
y
x
(b)
(s,x) is the shortest path using one edge. It is
also the shortest path from s to x.
50u
v
1
14/x
8/x
10
9
0
s
3
4
6
2
7
5
7/x
5/s
2
y
x
(c)
51u
v
1
13/y
8/x
10
9
0
s
3
4
6
2
7
5
7/x
5/s
2
y
x
(d)
52 (e)
53Time complexity of Dijkstras Algorithm
- Time complexity
- Extract-min O(V)
- Decrease-key Relax O(E)
- Use binary heap for priority heap
- Time complexity O((V E) log V)
- Use Fibonacci heap for priority heap
- Amortized time complexity O(V log V E)
- Greedy algorithm
- Any moment, select the lightest vertex in V-S to
add to set S