07 - Graphs

37
Graphs www.tudorgirba.com

description

I used this set of slides for the lecture on Graphs I gave at the University of Zurich for the 1st year students following the course of Formale Grundlagen der Informatik.

Transcript of 07 - Graphs

Page 1: 07 - Graphs

Graphs

www.tudorgirba.com

Page 2: 07 - Graphs
Page 3: 07 - Graphs
Page 4: 07 - Graphs
Page 5: 07 - Graphs

b

a

c d

e

f

g

G = (V, E)E = { {u,v} | u,v ∈ V}

Page 6: 07 - Graphs

b

a

c d

e

f

g

V = { a, b, c, d, e, f, g }

E = { {a,b}, {a,c}, {b,c}, {c,d}, {d,e}, {d,f}, {e,g}, {f,g} }

G = (V, E)E = { {u,v} | u,v ∈ V}

Page 7: 07 - Graphs

b

a

c d

e

f

g

G = (V, E)E = { {u,v} | u,v ∈ V}

V = { a, b, c, d, e, f, g }

E = { {a,b}, {a,c}, {b,c}, {c,d}, {d,e}, {d,f}, {e,g}, {f,g} }

Page 8: 07 - Graphs

a b c d e f g

a

b

c

d

e

f

g

0 1 1 0 0 0 0

0 0 1 0 0 0 0

0 0 0 1 0 0 0

0 0 0 0 1 1 0

0 0 0 0 0 0 1

0 0 0 0 0 0 1

0 0 0 0 0 0 0

b

a

c d

e

f

g

Page 9: 07 - Graphs

a b c d e f g

a

b

c

d

e

f

g

0 1 1 0 0 0 0

1 0 1 0 0 0 0

1 1 0 1 0 0 0

0 0 1 0 1 1 0

0 0 0 1 0 0 1

0 0 0 1 0 0 1

0 0 0 0 1 1 0

b

a

c d

e

f

g

Page 10: 07 - Graphs

a b c d e f g

a

b

c

d

e

f

g

0 1 1 0 0 0 0

1 0 1 0 0 0 0

1 1 0 1 0 0 0

0 0 1 0 1 1 0

0 0 0 1 0 0 1

0 0 0 1 0 0 1

0 0 0 0 1 1 0

b

a

c d

e

f

g

Degree of a node

2

2

3 3

2

2

2

2 3 3 2 2 2

Page 11: 07 - Graphs

a b c d e f g

a

b

c

d

e

f

g

0 2 3 0 0 0 0

0 0 1 0 0 0 0

0 0 0 2 0 0 0

0 0 0 0 5 4 0

0 0 0 0 0 0 3

0 0 0 0 0 0 3

0 0 0 0 0 0 0

1

3

22

5 3

34

Weighted graphs

b

a

c d

e

f

g

Page 12: 07 - Graphs

b

a

c

b

a

c

Not complete Complete

b

a

c

b

a

c

Page 13: 07 - Graphs

Bipartite Not bipartite

G = (V, E)∀ e={v,w} ∈ E, v ∈ V and w ∈ W.

Page 14: 07 - Graphs

b

a

c d

e

f

g

Path

Path: (b, a, c); Length (b, a, c) = 2Path: (b, d, f)

Cycle

Cycle: (f, g, e, d, f); Length (f, g, e, d, f) = 4

Page 15: 07 - Graphs

b

a

c d

e

f

g

Path

Path: (b, a, c); Length (b, a, c) = 2Path: (b, d, f)

Cycle

Cycle: (f, g, e, d, f); Length (f, g, e, d, f) = 4

Page 16: 07 - Graphs

b

a

c d

e

f

g

Path

Path: (b, a, c); Length (b, a, c) = 2Path: (b, d, f)

Cycle

Cycle: (f, g, e, d, f); Length (f, g, e, d, f) = 4

Page 17: 07 - Graphs

b

a

c d

e

f

g

b

a

c d

e

f

g

Loop-free Loop

Page 18: 07 - Graphs

b

a

c d

e

f

g

Page 19: 07 - Graphs

b

a

c d

e

f

g

Eulerian path

b

a

c d

e

f

g

Page 20: 07 - Graphs

b

a

c d

e

f

g

Eulerian path

b

a

c d

e

f

g

Hamiltonian path

Page 21: 07 - Graphs

b

a

c d

e

f

g

Spanning tree

G = (V, E).T ⊆ E.

d

e

f

g

a

c

Components

Page 22: 07 - Graphs

b

a

c d

e

f

g

Critical node

Critical edge

Page 23: 07 - Graphs

b

a

c d

e

f

g

Biconnected components

Page 24: 07 - Graphs

b

a

c d

e

f

gSubgraph Not subgraph

G = (V, E)G1 = (V1, E1)E1 = {{u,v}∈ E | u,v ∈ V1} ⊆ E.

Page 25: 07 - Graphs

b

a

c d

e

f

g

Weakly reachable = exists undirected path

Strongly reachable = exists directed path

Page 27: 07 - Graphs

E

F

DC

B

A

14

9

2

11

6

9

7

1015

Example: Dijkstra algorithm

Page 28: 07 - Graphs

E

F

DC

B

A

14

9

2

11

6

9

7

1015

0

∞∞

Example: Dijkstra algorithm

Page 29: 07 - Graphs

E

F

DC

B

A

14

9

2

11

6

9

7

1015

7

0

14

9 ∞

Example: Dijkstra algorithm

Page 30: 07 - Graphs

E

F

DC

B

A

14

9

2

11

6

9

7

1015

7

0

14

9 < 7 + 10 7 + 15 = 22

Example: Dijkstra algorithm

Page 31: 07 - Graphs

E

F

DC

B

A

14

9

2

11

6

9

7

1015

7

0

14 > 9 + 2

9 22 > 9 + 11

Example: Dijkstra algorithm

Page 32: 07 - Graphs

E

F

DC

B

A

14

9

2

11

6

9

7

1015

7

0

11

20

9 20

Example: Dijkstra algorithm

Page 33: 07 - Graphs

E

F

DC

B

A

14

9

2

11

6

9

7

1015

7

0

11

20 < 20 + 6

9 20

Example: Dijkstra algorithm

Page 34: 07 - Graphs

a b c d e f gabcdefg

0 2 3 0 0 0 00 0 1 0 0 0 00 0 0 2 0 0 00 0 0 0 5 4 00 0 0 0 0 0 30 0 0 0 0 0 30 0 0 0 0 0 0

1

3

22

5 3

34b

a

c d

e

f

g

Example: Floyd Warshall

Page 35: 07 - Graphs

a b c d e f gabcdefg

0 2 3 0 0 0 00 0 1 0 0 0 00 0 0 2 0 0 00 0 0 0 5 4 00 0 0 0 0 0 30 0 0 0 0 0 30 0 0 0 0 0 0

1

3

22

5 3

34b

a

c d

e

f

g

procedure FloydWarshall () for k := 1 to n for i := 1 to n for j := 1 to n path[i][j] = min ( path[i][j], path[i][k]+path[k][j] );

Example: Floyd Warshall

Page 36: 07 - Graphs

Example: Traveling salesman