Greedy 2 - PowerPoint PPT Presentation

About This Presentation
Title:

Greedy 2

Description:

Spanning tree T: A tree that includes all nodes from V. T = (V, E'), where E' E ... All spanning trees have same weight. Algorithmique Greedy 2. Jose Rolim. 6 ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 54
Provided by: admi1770
Category:
Tags: greedy | spanning

less

Transcript and Presenter's Notes

Title: Greedy 2


1
Greedy 2
  • Jose Rolim
  • University of Geneva

2
Examples Greedy
  • Minimum Spanning Trees
  • Shortest Paths Dijkstra

3
Definition 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

4
Example
5
MST
  • 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

6
Kruskals 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

7
Pseudo-code
8
The Greedy Algorithm in Action
4
2
6
1
5
3
7
9
The 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
10
The 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
11
The 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
12
The 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
13
The 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
14
The 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
15
The 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
16
The 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
17
The 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
18
The 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
19
The 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
20
The 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
21
The 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
22
The 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
23
The 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
24
The 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
25
The 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
26
The 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
27
Complexity
  • 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)

28
Prims 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 !

29
Issues
  • 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.

30
Pseudo-code
31
Prims 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.
32
Prims 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
33
Prims 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
34
Prims 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
35
Prims 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
36
Prims 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
37
Prims 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
38
Prims 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
39
Prims 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
40
Prims 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
41
Complexity
  • 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)

43
Problem 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.

44
Shortest 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).
45
Representing 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)

46
Dijkstras 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.

47
Implementation
  • a priority queue Q stores vertices in V-S, keyed
    by their d values.
  • the graph G is represented by adjacency lists.

48
u
v
s
y
x
(a)
49
u
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.
50
u
v
1
14/x
8/x
10
9
0
s
3
4
6
2
7
5
7/x
5/s
2
y
x
(c)
51
u
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)
53
Time 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
Write a Comment
User Comments (0)
About PowerShow.com