problem solving with algorithms and data structure - graphs
TRANSCRIPT
Problem Solving with Algorithms and Data Structure — Graph
Bruce Tsai
http://interactivepython.org/runestone/static/pythonds/Graphs/Objectives.html
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
Goal
Represent problem in form of graph
Use graph algorithms to solve problem
4
(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
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
Attributed Graph
Attribute
Characters of graph vertices or edges
weight, color, anything you want
7
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
Adjacency Matrix
9
from
to
Discussion Question
AB
C
DE
F
75
1
2
7
3
2
8
1
2
4
5
6
1
8
10
Adjacency List
11
Discussion Question
12
3
45
6
1015
57
7
10
713
5
12
Word Ladder Problem
Make change occur gradually by changing one letter at a time
FOOL -> POOL -> POLL -> POLE -> PALE -> SALE -> SAGE
13
Word Ladder Graph
14
O(V*L)
O(2E)
O(V+E)
Breadth First Search (BFS)
18
BFS Analysis
O(V+E)
19
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
Knight’s Tour Problem
21
Legal Move Graph
22
Knight Graph
24
Knight’s Tour
27
Choose Better Next Vertex
Visit hard-to-reach corners early
Use the middle square to hop across board only when necessary
28
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
O(V+E)
O(V)
queue
stack
Computational Complexity
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
decision problem set
optimization problemssearch problems
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
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
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
Start and Finish Time
Implementation
1. Call dfs(g)
2. Store vertices based on decreasing order of finish time
3. Return the ordered list
41
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
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
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
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
dfs(g)
dfs(g_t)
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
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
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
Proof of Implementation
Shortest Path Problem
Find the path with the smallest total weight along which to route any given message
53
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
O(V)
O(logV)
O(logV)O(E*logV)
O(V*logV)
O((V+E)*logV)
Broadcast Problem
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
Prim’s Spanning Tree Algorithm
Special case of generic minimum spanning tree
Find shortest paths in graph
62
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
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