problem solving with algorithms and data structure - graphs

68
Problem Solving with Algorithms and Data Structure — Graph Bruce Tsai

Upload: yi-lung-tsai

Post on 17-Jul-2015

849 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Problem Solving with Algorithms and Data Structure - Graphs

Problem Solving with Algorithms and Data Structure — Graph

Bruce Tsai

Page 2: Problem Solving with Algorithms and Data Structure - Graphs

http://interactivepython.org/runestone/static/pythonds/Graphs/Objectives.html

Page 3: Problem Solving with Algorithms and Data Structure - Graphs

Graph

A set of objects where some pairs of objects are connected by links

road map

airline flights

internet connection

skill tree

Tree is special kind of graph

Vertex(node)

Edge(arc)

3

Page 4: Problem Solving with Algorithms and Data Structure - Graphs

Goal

Represent problem in form of graph

Use graph algorithms to solve problem

4

Page 5: Problem Solving with Algorithms and Data Structure - Graphs

(Undirected) Graph

G = (V, E)

V = {v_1, v_2, v_3, v_4, v_5, v_6}

E = {(v_1, v_2), (v_1, v_5), (v_2, v_3), (v_2, v_5), (v_3, v_4), (v_4, v_5), (v_4, v_6)}

(v_1, v_5) == (v_5, v_1)

5

Page 6: Problem Solving with Algorithms and Data Structure - Graphs

Directed Graph

D = (V, A)

V = {v_0, v_1, v_2, v_3, v_4, v_5}

A = {(v_0, v_1), (v_0, v_5), (v_1, v_2), (v_2, v_3), (v_3, v_5), (v_3, v_4), (v_4, v_0), (v_5, v_2), (v_5, v_4)}

6

Page 7: Problem Solving with Algorithms and Data Structure - Graphs

Attributed Graph

Attribute

Characters of graph vertices or edges

weight, color, anything you want

7

Page 8: Problem Solving with Algorithms and Data Structure - Graphs

Path and Cycle

Path

Sequence of vertices that are connected by edges

[v_1, v_5, v_4, v_6]

Cycle

A path that starts and ends at same vertex

[v_2, v_3, v_4, v_5, v_2]8

Page 9: Problem Solving with Algorithms and Data Structure - Graphs

Adjacency Matrix

9

from

to

Page 10: Problem Solving with Algorithms and Data Structure - Graphs

Discussion Question

AB

C

DE

F

75

1

2

7

3

2

8

1

2

4

5

6

1

8

10

Page 11: Problem Solving with Algorithms and Data Structure - Graphs

Adjacency List

11

Page 12: Problem Solving with Algorithms and Data Structure - Graphs

Discussion Question

12

3

45

6

1015

57

7

10

713

5

12

Page 13: Problem Solving with Algorithms and Data Structure - Graphs

Word Ladder Problem

Make change occur gradually by changing one letter at a time

FOOL -> POOL -> POLL -> POLE -> PALE -> SALE -> SAGE

13

Page 14: Problem Solving with Algorithms and Data Structure - Graphs

Word Ladder Graph

14

Page 15: Problem Solving with Algorithms and Data Structure - Graphs
Page 16: Problem Solving with Algorithms and Data Structure - Graphs

O(V*L)

O(2E)

O(V+E)

Page 17: Problem Solving with Algorithms and Data Structure - Graphs
Page 18: Problem Solving with Algorithms and Data Structure - Graphs

Breadth First Search (BFS)

18

Page 19: Problem Solving with Algorithms and Data Structure - Graphs

BFS Analysis

O(V+E)

19

Page 20: Problem Solving with Algorithms and Data Structure - Graphs

Discussion Question - BFS

[1]

[1, 2, 3, 6]

[2, 3, 6]

[3, 6, 4]

[6, 4, 5]

[4, 5]

[5]

12

3

45

6

20

Page 21: Problem Solving with Algorithms and Data Structure - Graphs

Knight’s Tour Problem

21

Page 22: Problem Solving with Algorithms and Data Structure - Graphs

Legal Move Graph

22

Page 23: Problem Solving with Algorithms and Data Structure - Graphs
Page 24: Problem Solving with Algorithms and Data Structure - Graphs

Knight Graph

24

Page 25: Problem Solving with Algorithms and Data Structure - Graphs
Page 26: Problem Solving with Algorithms and Data Structure - Graphs
Page 27: Problem Solving with Algorithms and Data Structure - Graphs

Knight’s Tour

27

Page 28: Problem Solving with Algorithms and Data Structure - Graphs

Choose Better Next Vertex

Visit hard-to-reach corners early

Use the middle square to hop across board only when necessary

28

Page 29: Problem Solving with Algorithms and Data Structure - Graphs
Page 30: Problem Solving with Algorithms and Data Structure - Graphs

General Depth First Search (DFS)

Knight’s Tour is special case of depth first search

create the depth first tree

General depth first search is to search as deeply as possible

30

Page 31: Problem Solving with Algorithms and Data Structure - Graphs

O(V+E)

O(V)

Page 32: Problem Solving with Algorithms and Data Structure - Graphs

queue

stack

Page 33: Problem Solving with Algorithms and Data Structure - Graphs

Computational Complexity

Page 34: Problem Solving with Algorithms and Data Structure - Graphs

P versus NP problem

P: questions for which some algorithms can provide an answer in polynomial time

NP: questions for which an answer can be verified in polynomial time

Subset sum problem

{-7, -3, -2, 5, 8} -> {-3, -2, 5}

34

Page 35: Problem Solving with Algorithms and Data Structure - Graphs

decision problem set

optimization problemssearch problems

Page 36: Problem Solving with Algorithms and Data Structure - Graphs

NP-complete

Reduction: algorithm for transforming one problem into another problem

NP-complete: a problem p in NP is NP-Complete if every other problem in NP can be transformed into p in polynomial time

hardest problems in NP

36

Page 37: Problem Solving with Algorithms and Data Structure - Graphs

NP-hard

Decision problem: any arbitrary yes-or-no question on an infinite set of inputs

NP-hard: a decision problem H in NP-hard when for any problem L in NP, there is polynomial-time reduction from L to H

at least as hard as the hardest problems in NP

37

Page 38: Problem Solving with Algorithms and Data Structure - Graphs

Topological Sorting

Linear ordering of all vertices of a directed graph

(u, v) is an edge of directed graph and u is before v in ordering

38

Page 39: Problem Solving with Algorithms and Data Structure - Graphs
Page 40: Problem Solving with Algorithms and Data Structure - Graphs

Start and Finish Time

Page 41: Problem Solving with Algorithms and Data Structure - Graphs

Implementation

1. Call dfs(g)

2. Store vertices based on decreasing order of finish time

3. Return the ordered list

41

Page 42: Problem Solving with Algorithms and Data Structure - Graphs

Lemma

A directed graph G is acyclic if and only if a depth-first search of G yields no back edges.

Back edge: edge (u, v) connecting vertex u to an ancestor v in depth-first tree (v ↝ u → v)

=>: back edge exists then cycle exists

<=: cycle exists then back edge exists

42

Page 43: Problem Solving with Algorithms and Data Structure - Graphs

Proof of Implementation

For any pair of distinct vertices u, v ∈ V, if there is an edge in G from u to v, then f[v] < f[u].

Consider edge (u, v) explored by DFS, v cannot be gray since then that edge would be back edge. Therefore v must be white or black.

If v is white, v is descendant of u, and so f[v] < f[u]

If v is black, it has already been finished, so that f[v] < f[u]

43

Page 44: Problem Solving with Algorithms and Data Structure - Graphs

Strongly Connected Components

G = (V, E) and C ⊂ V

such that ∀ (v_i, v_j) ∈ C, path v_i to v_j and path v_j to v_i both exist

C is strongly connected components (SCC)

44

Page 45: Problem Solving with Algorithms and Data Structure - Graphs
Page 46: Problem Solving with Algorithms and Data Structure - Graphs

Implementation

1. Call dfs(g)

2. Compute transpose of g as g_t

3. Call dfs(g_t) but explore each vertex in decreasing order of finish time

4. Each tree of step 3 is a SCC

original transpose

46

Page 47: Problem Solving with Algorithms and Data Structure - Graphs

dfs(g)

Page 48: Problem Solving with Algorithms and Data Structure - Graphs

dfs(g_t)

Page 49: Problem Solving with Algorithms and Data Structure - Graphs

Lemma

Let C and C’ be distinct SCCs in directed graph G = (V, E), let u, v ∈ C, let u’, v’ ∈ C’, and suppose that there is a path u ↝ u’ in G. Then there cannot also be a path v’ ↝ v in G.

if v’ ↝ v exists then both u ↝ u’ ↝ v’ and v’ ↝ v ↝ u exist

C and C’ are not distinct SCCs49

Page 50: Problem Solving with Algorithms and Data Structure - Graphs

Lemma

Let C and C’ be distinct SCCs in directed graph G = (V, E). Suppose that there is an edge (u, v) ∈ E, where u ∈ C and v ∈ C’. Then f(C) > f(C’)

from x ∈ C to w ∈ C’

from y ∈ C’ cannot reach any vertex in C

50

Page 51: Problem Solving with Algorithms and Data Structure - Graphs

Corollary

Let C and C’ be distinct SCCs in directed graph G = (V, E). Suppose that there is an edge (u, v) ∈ ET, where u ∈ C and v ∈ C’. Then f(C) < f(C’).

(v, u) ∈ E then f(C’) < f(C)

51

Page 52: Problem Solving with Algorithms and Data Structure - Graphs

Proof of Implementation

Page 53: Problem Solving with Algorithms and Data Structure - Graphs

Shortest Path Problem

Find the path with the smallest total weight along which to route any given message

53

Page 54: Problem Solving with Algorithms and Data Structure - Graphs

Dijkstra’s Algorithm

Iterative algorithm providing the shortest path from one particular starting node to all other nodes in graph

Path distance, priority queue

54

Page 55: Problem Solving with Algorithms and Data Structure - Graphs
Page 56: Problem Solving with Algorithms and Data Structure - Graphs
Page 57: Problem Solving with Algorithms and Data Structure - Graphs
Page 58: Problem Solving with Algorithms and Data Structure - Graphs
Page 59: Problem Solving with Algorithms and Data Structure - Graphs

O(V)

O(logV)

O(logV)O(E*logV)

O(V*logV)

O((V+E)*logV)

Page 60: Problem Solving with Algorithms and Data Structure - Graphs

Broadcast Problem

Page 61: Problem Solving with Algorithms and Data Structure - Graphs

Minimum Spanning Tree

T is acyclic subset of E that connects all vertices in V

The sum of weight of edges in T is minized

61

Page 62: Problem Solving with Algorithms and Data Structure - Graphs

Prim’s Spanning Tree Algorithm

Special case of generic minimum spanning tree

Find shortest paths in graph

62

Page 63: Problem Solving with Algorithms and Data Structure - Graphs
Page 64: Problem Solving with Algorithms and Data Structure - Graphs
Page 65: Problem Solving with Algorithms and Data Structure - Graphs

Kruskal’s Algorithm

Sort edges in E into ascending order by weight

For (u, v) ∈ E, if set of u not equal to set of v

A ← A ∪ {(u, v)}

65

Page 66: Problem Solving with Algorithms and Data Structure - Graphs
Page 67: Problem Solving with Algorithms and Data Structure - Graphs
Page 68: Problem Solving with Algorithms and Data Structure - Graphs

Referencehttp://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/AproxAlgor/TSP/tsp.htm

http://en.wikipedia.org/wiki/Graph_%28mathematics%29

http://en.wikipedia.org/wiki/P_versus_NP_problem

http://en.wikipedia.org/wiki/NP-complete

http://en.wikipedia.org/wiki/NP-hard

http://en.wikipedia.org/wiki/Reduction_(complexity)

http://en.wikipedia.org/wiki/Knight%27s_tour

http://staff.ustc.edu.cn/~csli/graduate/algorithms/book6/chap24.htm

Introduction to Algorithms, 2nd edition