powerpoint presentationcsl.skku.edu/uploads/sse2025s17/lectur… · ppt file · web view ·...

20
Graph Algorithm

Upload: phamcong

Post on 11-Apr-2018

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: PowerPoint Presentationcsl.skku.edu/uploads/SSE2025S17/Lectur… · PPT file · Web view · 2017-05-23Title: PowerPoint Presentation Author: joon Last modified by: Joonwon Lee Created

Graph Algorithm

Page 2: PowerPoint Presentationcsl.skku.edu/uploads/SSE2025S17/Lectur… · PPT file · Web view · 2017-05-23Title: PowerPoint Presentation Author: joon Last modified by: Joonwon Lee Created

Recap A topological sort of a DAG GG is a linear ordering of all its vertices such that if GG contains a link (u,v), then node u appears before node v in the ordering

b

c a

d

b

c a

d

1

2 3

4

Page 3: PowerPoint Presentationcsl.skku.edu/uploads/SSE2025S17/Lectur… · PPT file · Web view · 2017-05-23Title: PowerPoint Presentation Author: joon Last modified by: Joonwon Lee Created

Algorithm Example find source nodes (indegree = 0)

if there is no such node, the graph is NOT DAG

c

a

b

e

d

f

in_deg=1

in_deg=0

in_deg=2

in_deg=1

in_deg=3

in_deg=1 Queue

Sorted: - Sorted: -

c

Page 4: PowerPoint Presentationcsl.skku.edu/uploads/SSE2025S17/Lectur… · PPT file · Web view · 2017-05-23Title: PowerPoint Presentation Author: joon Last modified by: Joonwon Lee Created

Degrees Summary number of edges connected to a vertex for undirected graphs

sum of all degrees = 2 X edges the number of nodes with odd numbered degrees is even?

for directed graph sum of in-degree = sum of out-degree

Page 5: PowerPoint Presentationcsl.skku.edu/uploads/SSE2025S17/Lectur… · PPT file · Web view · 2017-05-23Title: PowerPoint Presentation Author: joon Last modified by: Joonwon Lee Created

Conectivity connected

there exists a path between every pair of vertices articulation vertex

deleting this vertex makes the graph disconnected a graph without any such vertex is biconnected deleting a bridge edge makes the graph disconnected

Page 6: PowerPoint Presentationcsl.skku.edu/uploads/SSE2025S17/Lectur… · PPT file · Web view · 2017-05-23Title: PowerPoint Presentation Author: joon Last modified by: Joonwon Lee Created

Cycles A tree does not have a cycle Eulerian cycle

a tour that visits every edge exactly once Hamiltonian cycle (path)

a tour that visits every vertex exactly once

Page 7: PowerPoint Presentationcsl.skku.edu/uploads/SSE2025S17/Lectur… · PPT file · Web view · 2017-05-23Title: PowerPoint Presentation Author: joon Last modified by: Joonwon Lee Created

Given a graph G = (V, E) and tree T = (V, E) E E for all (u, v) in E u, v V for all connected graph, there exists a spanning tree

A spanning tree can be constructed using DFS or BFS

s

2

5

4

7

8

3 6 9

s

2

5

4

7

8

3 6 9

0

1

1

1

2

2

3

3

3

Spanning Tree

Page 8: PowerPoint Presentationcsl.skku.edu/uploads/SSE2025S17/Lectur… · PPT file · Web view · 2017-05-23Title: PowerPoint Presentation Author: joon Last modified by: Joonwon Lee Created

Minimal Spanning Tree sum of edge weights is minimal if there is no weight

the number of edges is minimal why is it so important?

search space is minimal for most problems

Page 9: PowerPoint Presentationcsl.skku.edu/uploads/SSE2025S17/Lectur… · PPT file · Web view · 2017-05-23Title: PowerPoint Presentation Author: joon Last modified by: Joonwon Lee Created

Example of MST: Prim’s Algorithm

Page 10: PowerPoint Presentationcsl.skku.edu/uploads/SSE2025S17/Lectur… · PPT file · Web view · 2017-05-23Title: PowerPoint Presentation Author: joon Last modified by: Joonwon Lee Created

1. Vertex D has been chosen as a starting point① Vertices A, B, E, F are connected to D through a single edge.② A is the nearest to D and thus chosen as the 2nd vertex along with the edge AD

Page 11: PowerPoint Presentationcsl.skku.edu/uploads/SSE2025S17/Lectur… · PPT file · Web view · 2017-05-23Title: PowerPoint Presentation Author: joon Last modified by: Joonwon Lee Created

2. The next vertex chosen is the vertex nearest to either D or A. So the vertex F is chosen along with the edge DF

Page 12: PowerPoint Presentationcsl.skku.edu/uploads/SSE2025S17/Lectur… · PPT file · Web view · 2017-05-23Title: PowerPoint Presentation Author: joon Last modified by: Joonwon Lee Created

3. same as 2, Vertex B is chosen.

Page 13: PowerPoint Presentationcsl.skku.edu/uploads/SSE2025S17/Lectur… · PPT file · Web view · 2017-05-23Title: PowerPoint Presentation Author: joon Last modified by: Joonwon Lee Created

4. among C, E, G, E is chosen.

Page 14: PowerPoint Presentationcsl.skku.edu/uploads/SSE2025S17/Lectur… · PPT file · Web view · 2017-05-23Title: PowerPoint Presentation Author: joon Last modified by: Joonwon Lee Created

5. among C and G, C is chosen.

Page 15: PowerPoint Presentationcsl.skku.edu/uploads/SSE2025S17/Lectur… · PPT file · Web view · 2017-05-23Title: PowerPoint Presentation Author: joon Last modified by: Joonwon Lee Created

6. G is the only remaining vertex.

Page 16: PowerPoint Presentationcsl.skku.edu/uploads/SSE2025S17/Lectur… · PPT file · Web view · 2017-05-23Title: PowerPoint Presentation Author: joon Last modified by: Joonwon Lee Created

7. The finally obtained minimum spanning tree the total weight is 39

Page 17: PowerPoint Presentationcsl.skku.edu/uploads/SSE2025S17/Lectur… · PPT file · Web view · 2017-05-23Title: PowerPoint Presentation Author: joon Last modified by: Joonwon Lee Created

Kruskal’s algorithm

Page 18: PowerPoint Presentationcsl.skku.edu/uploads/SSE2025S17/Lectur… · PPT file · Web view · 2017-05-23Title: PowerPoint Presentation Author: joon Last modified by: Joonwon Lee Created

Dijkstra’s Algorithm Goal: Find the shortest path from s to ts

3

t

2

6

7

45

24

18 2

9

14

15 5

30

20

44

16

11

6

19

6

Page 19: PowerPoint Presentationcsl.skku.edu/uploads/SSE2025S17/Lectur… · PPT file · Web view · 2017-05-23Title: PowerPoint Presentation Author: joon Last modified by: Joonwon Lee Created

All Pairs Shortest Paths Use Dijkstra’s method for all the vertices

complexity? Floyd’s method

Given the adjacency matrix with vertices numbered (1..n)

Page 20: PowerPoint Presentationcsl.skku.edu/uploads/SSE2025S17/Lectur… · PPT file · Web view · 2017-05-23Title: PowerPoint Presentation Author: joon Last modified by: Joonwon Lee Created

Network Flow Think edges as pipes

what’s the maximum flow from node 1 to node 5?