• 沒有找到結果。

# Algorithm Design and AnalysisGraph Algorithms (2)

N/A
N/A
Protected

Share "Algorithm Design and AnalysisGraph Algorithms (2)"

Copied!
68
0
0

(1)

### Algorithm Design and Analysis

Slides credited from Hsueh-I Lu, Hsu-Chun Hsiao, & Michael Tsai

(2)

2

(3)

### • DFS Applications

• Connected Components

• Strongly Connected Components

• Topological Sorting

### • Minimal Spanning Trees (MST)

• Boruvka’s Algorithm

• Kruskal’s Algorithm

• Prim’s Algorithm

(4)

## Depth-First Search

4

Textbook Chapter 22.3 – Depth-first search

(5)

1

2 3

4 8

9 12 13

14

5 6

7

10 11

(6)

## Connected Components

6

(7)

### • Output: a connected component of 𝐺

• a maximal subset 𝑈 of 𝑉 s.t. any two nodes in 𝑈 are connected in 𝐺

(8)

### Connected Components

8

10 1

2

5

3 4

6

7

8 9

Time Complexity:

BFS and DSF both find the connected components with the same complexity

(9)

(10)

## Strongly Connected Components

10

Textbook Chapter 22.5 – Strongly connected components

(11)

### • Output: a connected component of 𝐺

• a maximal subset 𝑈 of 𝑉 s.t. any two nodes in 𝑈 are reachable in 𝐺

1

2

4

7

8

Why must the strongly connected components of a graph be disjoint?

(12)

12

(13)

1

2

4

6

5

1

2

4

6

5

(14)

14

1

3

2

6 5

4 1

2

4

5 3

6

(15)

### Algorithm Correctness

• Assume that 𝑣, 𝑤 is an incoming edge to 𝐶.

• Since 𝐶 is a strongly connected component of 𝐺, there cannot be any path from any node of 𝐶 to 𝑣 in 𝐺.

• Therefore, the finish time of 𝑣 has to be larger than any node in 𝐶, including 𝑢. → Lemma

Let 𝐶 be the strongly connected component of 𝐺 (and 𝐺𝑇) that contains the node 𝑢 with the largest finish time 𝑢. 𝑓. Then 𝐶 cannot have any incoming edge from any node of 𝐺 not in 𝐶.

𝑢

C 𝑤

𝑣

(16)

### • Practice to prove using induction

16

𝑢

G

C

𝑢

GT

C Theorem

By continuing the process from the vertex 𝑢 whose finish time 𝑢. 𝑓 is the largest excluding those in 𝐶, the algorithm returns the strongly connected components.

(17)

1

3

2

6

4

(18)

18

1

3

2

6 5

4

(19)

(20)

20

(21)

## Topological Sort

Textbook Chapter 22.4 – Topological sort

(22)

22

1

2

3

5 4

6 1

2

3

5 4

6

(23)

1

2

3

(24)

24

(25)

a b d

f c e

a

b

d

(26)

### • Output the nodes in decreasing order of their finish time.

26

DFS(G)

for each vertex u in G.V u.color = WHITE

u.pi = NIL time = 0

for each vertex u in G.V if u.color == WHITE

DFS-VISIT(G, u)

DFS-Visit(G, u) time = time + 1 u.d = time

u.color = GRAY

for each v in G.Adj[u] (outgoing) if v.color == WHITE

v.pi = u

DFS-VISIT(G, v) u.color = BLACK

time = time + 1

u.f = time // finish time

(27)

a

b

d

f c

e

a b d

f c e

1

4 2

5

(28)

28

a

b

d

f c

e

f b d

a c e

1

2

3 4

6

5

(29)

### • Output the nodes in decreasing order of their finish time.

• As each vertex is finished, insert it onto the front of a linked list

• Return the linked list of vertices

DFS(G)

for each vertex u in G.V u.color = WHITE

u.pi = NIL

DFS-Visit(G, u) time = time + 1 u.d = time

u.color = GRAY

if v.color == WHITE v.pi = u

Time Complexity:

(30)

### • Proof

• →: suppose there is a back edge 𝑢, 𝑣

• 𝑣 is an ancestor of 𝑢 in DFS forest

• There is a path from 𝑣 to 𝑢 in 𝐺 and 𝑢, 𝑣 completes the cycle

•  : suppose there is a cycle 𝑐

• Let 𝑣 be the first vertex in 𝑐 to be discovered and 𝑢 is a predecessor of 𝑣 in 𝑐

• Upon discovering 𝑣 the whole cycle from 𝑣 to 𝑢 is WHITE

• At time 𝑣. 𝑑, the vertices of 𝑐 form a path of white vertices from 𝑣 to 𝑢

• By the white-path theorem, vertex 𝑢 becomes a descendant of 𝑣 in the DFS forest

• Therefore, 𝑢, 𝑣 is a back edge

30

Lemma 22.11

A directed graph is acyclic  a DFS yields no back edges.

White Path Theorem: In a DFS forest of 𝐺, 𝑣 is a descendant of 𝑢 in the forest  at the time 𝑢. 𝑑 that the search discovers 𝑢, there is a path from 𝑢 to 𝑣 in 𝐺 consisting entirely of WHITE vertices

(31)

### • Proof

• When 𝑢, 𝑣 is being explored, 𝑢 is GRAY and there are three cases for 𝑣:

• Case 1 – GRAY

• 𝑢, 𝑣 is a back edge (contradicting Lemma 22.11), so 𝑣 cannot be GRAY

• Case 2 – WHITE

• 𝑣 becomes descendant of 𝑢

• 𝑣 will be finished before 𝑢

Theorem 22.12

The algorithm produces a topological sort of the input DAG. That is, if 𝑢, 𝑣 is a directed edge (from 𝑢 to 𝑣) of 𝐺, then 𝑢. 𝑓 > 𝑣. 𝑓.

(32)

32

(33)

(34)

## Minimal Spanning Tree (MST)

34

Textbook Chapter 23 – Minimal Spanning Trees

(35)

### • Definition

• a subgraph that is a tree and connects all vertices

• Exactly 𝑛 − 1 edges

• Acyclic

• There can be many spanning trees of a graph

### • BFS and DFS also generate spanning trees

• BFS tree is typically “short and bushy”

• DFS tree is typically “long and stringy”

2

1 1

2 2

3

1

(36)

### • Output: a spanning tree 𝑇 of 𝐺 with minimum 𝑤(𝑇)

36

2

1

1

1

2

2 2

3

1

WLOG: we may assume that all edge weights are distinct

(37)

### • Q: What if the graph contains edges with negative weights?

Trivial

Add a large constant to every edge; a MST remains the same

(38)

### Uniqueness of MST

• Suppose there are two MSTs 𝐴 and 𝐵

• Let 𝑒 be the least-weight edge in 𝐴⋃𝐵 and 𝑒 is not in both

• WLOG, assume 𝑒 is in 𝐴

• Add 𝑒 to 𝐵; 𝑒 ⋃𝐵 contains a cycle 𝐶

• B includes at least one edge 𝑒′ that is not in 𝐴 but on 𝐶

• Replacing 𝑒′ with 𝑒 yields a MST with less cost

38

### Theorem: MST is unique if all edge weights are distinct

If edge weights are not all distinct, then the (multi-)set of weights in MST is unique

(39)

## Borůvka’s Algorithm

(40)

### • Gave an 𝑂 𝑚 log 𝑛 time algorithm

• The original paper was written in Czech in 1926

• The purpose was to efficiently provide electric coverage of Bohemia

40

(41)

### • Repeat the below procedure until the resulting graph becomes a single node

• For each node 𝑢, mark its lightest incident edge

• From the marked edges form a forest 𝐹, add the edges of 𝐹 into the set of edges to be reported

• Contract each maximal subtree of 𝐹 into a single node

(42)

42

2.1

1.3

2.3

1.2 2.2

3.1

2.4 3

1

1.5

1.4

2.6

2.7 2.5

3.2

5

3.3 4

4.1 5.1

(43)

### Algorithm Correctness

• An MST 𝑇 of 𝐺 that does not contain 𝑢, 𝑣

• A cycle 𝐶 = 𝑇 ∪ 𝑢, 𝑣 contains an edge 𝑢, 𝑤 in 𝐶 that has larger weight than 𝑢, 𝑣

• 𝑇 = 𝑇 ∪ 𝑢, 𝑣 \ 𝑢, 𝑤 must be a spanning tree of 𝐺 lighter than 𝑇

(44)

### • The recurrence relation

• We check all edges in each phase

• After each contraction phase, the number of nodes is reduced by at least one half

44

(45)

### Cycle Property

• Suppose 𝑒 is in the MST

• Removing 𝑒 disconnects the MST into two components T1 and T2

• There exists another edge 𝑒′ in 𝐶 that can reconnect T1 and T2

• Since 𝑤 𝑒’ < 𝑤(𝑒), the new tree has a lower weight

(46)

### Cut Property

• Suppose 𝑒 is not in the current MST

• Adding 𝑒 creates a cycle in the MST

• There exists another edge 𝑒′ in 𝐶 that can break the cycle

• Since 𝑤 𝑒’ > 𝑤(𝑒), the new tree has a lower weight

46

### Let 𝐶 be a cut in the graph, and let 𝑒 be the edge with the minimum cost in 𝐶. Then the MST contains 𝑒.

• Cut = a partition of the vertices

• For simplicity, assume all edge weights are distinct

(47)

## Kruskal’s Algorithm

Textbook Chapter 23.2 – The algorithms of Kruskal and Prim

(48)

### • For each node 𝑢

• Make-set(𝑢): create a set consisting of 𝑢

### • if Find-set(𝑢) ≠Find-set(𝑣) (i.e., 𝑢 and 𝑣 are not in the same set) then

• Output edge 𝑢, 𝑣

• Union(𝑢, 𝑣): union the sets containing 𝑢 and 𝑣 into a single set

48

(49)

2.1

1.3 1.2

2.2

1

1.5

1.4 2.7

2.5 3.2

4.1 3.1 2.3

2.4 3

5 2.6 3.3 4

5.1

(50)

### Kruskal’s Algorithm Correctness

50

The lightest edge incident to a vertex must be in the MST

(51)

### • Consider whether adding 𝑒 creates a cycle:

• If adding 𝑒 to 𝑇 creates a cycle 𝐶

• Then 𝑒 is the max weight edge in 𝐶

• The cycle property ensures that 𝑒 is not in the MST

• If adding 𝑒 = 𝑢, 𝑣 to 𝑇 does not create a cycle

• Before adding 𝑒, the current MST can be divided into two trees T1 and T2 such that 𝑢 in T1 and 𝑉 in T2

• 𝑒 is the minimum-cost edge on the cut of T1 and T2

• The cut property ensures that 𝑒 is in the MST

(52)

### • Disjoint-set data structure with union-by-rank (Textbook Ch. 21)

• MAKE-SET:

• FIND-SET:

• UNION:

• The amortized cost of 𝑚 operations on 𝑛 elements (Exercise 21.4-4):

### • Total complexity:

52 MST-KRUSKAL(G, w) // w = weights

A = empty // edge set of MST for v in G.V

MAKE-SET(v)

sort edges of G.E into non-decreasing order by weight w for (u, v) in G.E, taken in non-decreasing order by weight

if FIND-SET(u) ≠ FIND-SET(v) A = A ∪ {u, v}

UNION(u, v) return A

(53)

## Prim’s Algorithm

Textbook Chapter 23.2 – The algorithms of Kruskal and Prim

(54)

### • For 𝑖 = 1 to 𝑛 − 1

• add the least-weighted edge incident to the current subtree 𝑇 that does not incur a cycle

54

(55)

13

50 11

7

2

8 12

9

10 40 14

1 3 6

20

(56)

56

13

50 11

7

2

8 12

9

10 40 14

1 3 6

20

(57)

13

50 11

7

2

8 12

9

10 40 14

1 3 6

20

(58)

58

13

50 11

7

2

8 12

9

10 40 14

1 3 6

20

(59)

13

50 11

7

2

8 12

9

10 40 14

1 3 6

20

(60)

60

13

50 11

7

2

8 12

9

10 40 14

1 3 6

20

(61)

13

50 11

7

2

8 12

9

10 40 14

1 3 6

20

(62)

62

13

50 11

7

2

8 12

9

10 40 14

1 3 6

20

(63)

13

50 11

7

2

8 12

9

10 40 14

1 3 6

20

(64)

### Prim’s Algorithm Correctness

64

The lightest edge incident to a vertex must be in the MST

(65)

### • Binary min-heap (Textbook Ch. 6)

MST-PRIM(G, w, r) // w = weights, r = root for u in G.V

u.key = ∞ u.π = NIL r.key = 0 Q = G.V

while Q ≠ empty

u = EXTRACT-MIN(Q) for v in G.adj[u]

if v ∈ Q and w(u, v) < v.key v.π = u

v.key = w(u, v) // DECREASE-KEY

(66)

### • Fibonacci heap (Textbook Ch. 19)

• BUILD-MIN-HEAP:

• EXTRACT-MIN: (amortized)

• DECREASE-KEY: (amortized)

### • Total complexity:

66 MST-PRIM(G, w, r) // w = weights, r = root

for u in G.V u.key = ∞ u.π = NIL r.key = 0 Q = G.V

while Q ≠ empty

u = EXTRACT-MIN(Q) for v in G.adj[u]

if v ∈ Q and w(u, v) < v.key v.π = u

v.key = w(u, v) // DECREASE-KEY

(67)

### • Minimal Spanning Trees (MST)

• Boruvka’s Algorithm:

• Kruskal’s Algorithm:

• Prim’s Algorithm: with binary min-heap

• Prim’s Algorithm: with Fabonacci heap

(68)

## Question?

Important announcement will be sent to

@ntu.edu.tw mailbox & post to the course website

• makes a locally optimal choice in the hope that this choice will lead to a globally optimal solution.. • not always yield optimal solution; may end up at

Textbook Chapter 33.4 – Finding the closest pair of points.. Closest Pair of

✓ Combining an optimal solution to the subproblem via greedy can arrive an optimal solution to the original problem.. Prove that there is always an optimal solution to the

Textbook Chapter 4.3 – The substitution method for solving recurrences Textbook Chapter 4.4 – The recursion-tree method for solving recurrences Textbook Chapter 4.5 – The master

jobs

Previous efficient algorithm, Forward Algorithm, needs O(m 3/2 ) time and O(m) space. To develop algorithms on random access machines, we come up with

▪ Step 2: Run DFS on the transpose

He proposed a ﬁxed point algorithm and a gradient projection method with constant step size based on the dual formulation of total variation.. These two algorithms soon became