Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth...

56
Graph Theory Kayman Lui 20-09-2007

Transcript of Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth...

Page 1: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Graph Theory

Kayman Lui20-09-2007

Page 2: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Overview

• Graph– Notation and Implementation– Tree

• Depth First Search (DFS)– DFS Forests

• Topology Sort (T-Sort)• Strongly Connected Component (SCC)

• Breadth First Search (BFS)• Graph Modeling• Variations of BFS and DFS

– Bidirectional Search (BDS)– Iterative Deepening Search(IDS)

Page 3: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

What is a graph?

• A set of vertices and edges– Directed/Undirected– Weighted/Unweighted– Cyclic/Acyclic

vertex

edge

Page 4: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Representation of Graph

• Adjacency Matrix– A V x V array, with matrix[i][j]

storing whether there is an edge between the ith vertex and the jth vertex

• Adjacency Linked List– One linked list per vertex, each

storing directly reachable vertices

• Edge List

Page 5: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Representation of Graphs

Adjacency Matrix

Adjacency Linked List

Edge List

Memory Storage

O(V2) O(V+E) O(V+E)

Check whether (u,v) is an edge

O(1) O(deg(u)) O(deg(u))

Find all adjacent vertices of a vertex u

O(V) O(deg(u)) O(deg(u))

deg(u): the number of edges connecting vertex u

Page 6: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Trees and related terms

root

siblings

descendents children

ancestors

parent

Page 7: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

What is a tree?

• A tree is an undirected simple graph G that satisfies any of the following equivalent conditions:

– G is connected and has no simple cycles. – G has no simple cycles and, if any edge is

added to G, then a simple cycle is formed. – G is connected and, if any edge is removed

from G, then it is not connected anymore. – Any two vertices in G can be connected by a

unique simple path. – G is connected and has n − 1 edges. – G has no simple cycles and has n − 1 edges.

Page 8: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Graph Searching

• Given: a graph• Goal: visit all (or some) vertices

and edges of the graph using some strategy (the order of visit is systematic)

• DFS, BFS are examples of graph searching algorithms

• Some shortest path algorithms and spanning tree algorithms have specific visit order

Page 9: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Depth-First Search (DFS)

• Strategy: Go as far as you can (if you have not visit there), otherwise, go back and try another way– Example: a person want to visit a

place, but do not know the path

Page 10: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

F

A

BC

D

E

DFS (Demonstration)

unvisited

visited

Page 11: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

DFS (pseudo code)

DFS (vertex u) {mark u as visitedfor each vertex v directly reachable from u

if v is unvisitedDFS (v)

}

• Initially all vertices are marked as unvisited

Page 12: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

“Advanced” DFS

• Apart from just visiting the vertices, DFS can also provide us with valuable information

• DFS can be enhanced by introducing:– birth time and death time of a vertex

• birth time: when the vertex is first visited• death time: when we retreat from the

vertex

– DFS tree– parent of a vertex

Page 13: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

DFS spanning tree / forest

• A rooted tree• The root is the start vertex• If v is first visited from u, then u is the

parent of v in the DFS tree• Edges are those in forward direction of

DFS, ie. when visiting vertices that are not visited before

• If some vertices are not reachable from the start vertex, those vertices will form other spanning trees (1 or more)

• The collection of the trees are called forest

Page 14: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

A

F

B

C

D

E

GH

DFS forest (Demonstration)

unvisited

visited

visited (dead)

A B C D E F G H

birth

death

parent

A

B

C

F

E

D

G

1 2 3 13 10 4 14

12 9 8 16 11 5 15

H

6

7

- A B - A C D C

Page 15: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

DFS (pseudo code)

DFS (vertex u) {mark u as visited

time time+1; birth[u]=time;

for each vertex v directly reachable from u

if v is unvisitedparent[v]=u

DFS (v) time time+1; death[u]=time;

}

Page 16: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Classification of edges

• Tree edge• Forward edge• Back edge• Cross edge

• Question: which type of edges is always absent in an undirected graph?

A

B

C

F

E

D

G

H

Page 17: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Determination of edge types

• How to determine the type of an arbitrary edge (u, v) after DFS?

• Tree edge– parent [v] = u

• Forward edge– not a tree edge; and– birth [v] > birth [u]; and– death [v] < death [u]

• How about back edge and cross edge?

Page 18: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Determination of edge types

Tree edge Forward Edge Back Edge Cross Edge

parent [v] = u not a tree edgebirth[v] > birth[u]death[v] < death[u]

birth[v] < birth[u]death[v] > death[u]

birth[v] < birth[u]death[v] < death[u]

Page 19: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Applications of DFS Forests

• Topological sorting (Tsort)• Strongly-connected components

(SCC)• Some more “advanced” algorithms

Page 20: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Topological Sort

• Topological order: A numbering of the vertices of a directed acyclic graph such that every edge from a vertex numbered i to a vertex numbered j satisfies i<j

• Topological Sort: Finding the topological order of a directed acyclic graph

Page 21: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Example

• Assembly Line– In a factory, there is several process. Some

need to be done before others. Can you order those processes so that they can be done smoothly?

• Studying Order– Louis is now studying ACM materials. There

are many topics. He needs to master some basic topics before understanding those advanced one. Can you help him to plan a smooth study plan?

Page 22: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

T-sort Algorithm

• If the graph has more then one vertex that has indegree 0, add a vertice to connect to all indegree-0 vertices

• Let the indegree 0 vertice be s• Use s as start vertice, and

compute the DFS forest• The death time of the vertices

represent the reverse of topological order

Page 23: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Tsort (Demonstration)

S

D

B

E F

C

G

A

S A B C D E F G

birth

death

G C F B A E D

1 2 3 4 5

67

8

91011

12 13

141516

D E A B F C G

Page 24: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Strongly-connected components (SCC)

• A graph is strongly-connected if– for any pair of vertices u and v, one

can go from u to v and from v to u.

• Informally speaking, an SCC of a graph is a subset of vertices that– forms a strongly-connected

subgraph– does not form a strongly-connected

subgraph with the addition of any new vertex

Page 25: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

SCC (Illustration)

Page 26: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

SCC (Algorithm)

• Compute the DFS forest of the graph G to get the death time of the vertices

• Reverse all edges in G to form G’• Compute a DFS forest of G’, but

always choose the vertex with the latest death time when choosing the root for a new tree

• The SCCs of G are the DFS trees in the DFS forest of G’

Page 27: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

A

F

B

C

D

GH

SCC (Demonstration)

A

F

B

C

D

E

GH

A B C D E F G H

birth

death

parent

1 2 3 13 10 4 14

12 9 8 16 11 5 15

6

7

- A B - A C D C

D

G

A E B

F

C

H

Page 28: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

SCC (Demonstration)

D

G

A E B

F

C

H

A

F

B

C

D

GH

E

Page 29: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

DFS Summary

• DFS spanning tree / forest

• We can use birth time and death time in DFS spanning tree to do varies things, such as Tsort, SCC

• Notice that in the previous slides, we related birth time and death time. But in the discussed applications, birth time and death time can be independent, ie. birth time and death time can use different time counter

Page 30: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Breadth-First Search (BFS)

• Instead of going as far as possible, BFS goes through all the adjacent vertices before going further (ie. spread among next vertices)– Example: set a house on fire, the fire

will spread through the house

• BFS makes use of a queue to store visited (but not dead) vertices, expanding the path from the earliest visited vertices.

Page 31: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

A

B

C

D

E

F

G

H

I

J

BFS (Demonstration)

unvisited

visited

visited (dequeued)

Queue: A B C F D E H G J I

Page 32: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

BFS (Pseudo code)

while queue not emptydequeue the first vertex u from queuefor each vertex v directly reachable from u

if v is unvisitedenqueue v to queuemark v as visited

• Initially all vertices except the start vertex are marked as unvisited and the queue contains the start vertex only

Page 33: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Applications of BFS

• Shortest paths finding• Flood-fill

Page 34: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Flood Fill

• An algorithm that determines the area connected to a given node in a multi-dimensional array

• Start BFS from the given node, counting the total number of nodes visited

• It can also be handled by DFS

Page 35: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Comparisons of DFS and BFS

DFS BFS

Depth-first Breadth-first

Stack Queue

Does not guarantee shortest paths

Guarantees shortest paths

Page 36: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

What is graph modeling?

• Conversion of a problem into a graph problem

• Sometimes a problem can be easily solved once its underlying graph model is recognized

• Graph modeling appears in many ACM problems

Page 37: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Basics of graph modeling

• A few steps:– identify the vertices and the edges– identify the objective of the problem– state the objective in graph terms– implementation:

• construct the graph from the input instance

• run the suitable graph algorithms on the graph

• convert the output to the required format

Page 38: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Examples(1)

• Given a grid maze with obstacles, find a shortest path between two given points

start

goal

Page 39: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Examples (2)

• A student has the phone numbers of some other students

• Suppose you know all pairs (A, B) such that A has B’s number

• Now you want to know Alpha number, what is the minimum number of calls you need to make?

Page 40: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Examples (2)

• Vertex: student• Edge: whether A has B’s number• Add an edge from A to B if A has

B’s number• Problem: find a shortest path

from your vertex to Alpha’s vertex

Page 41: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Teacher’s Problem

• Question: A teacher wants to distribute sweets to students in an order such that, if student u tease student v, u should not get the sweet before v

• Vertex: student• Edge: directed, (v,u) is a directed

edge if student v tease u• Algorithm: T-sort

Page 42: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Variations of BFS and DFS

• Bidirectional Search (BDS)• Iterative Deepening Search(IDS)

Page 43: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Bidirectional search (BDS)

• Searches simultaneously from both the start vertex and goal vertex

• Commonly implemented as bidirectional BFS

start goal

Page 44: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

BDS Example: Bomber Man (1 Bomb)

• find the shortest path from the upper-left corner to the lower-right corner in a maze using a bomb. The bomb can destroy a wall.

S

E

Page 45: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Bomber Man (1 Bomb)

S

E

1 2 3

4

1234

5

4

Shortest Path length = 8

5

6 67 78 8

9

9

10

10

11

11

12

12 12

13

13

Page 46: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Iterative deepening search (IDS)

• Iteratively performs DFS with increasing depth bound

• Shortest paths are guaranteed

Page 47: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

IDS

Page 48: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

IDS (pseudo code)

DFS (vertex u, depth d) {mark u as visitedif (d>0)

for each vertex v directly reachable from uif v is unvisited

DFS (v,d-1)}

i=0Do {

DFS(start vertex,i)Increment i

}While (target is not found)

Page 49: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

IDS Complexity (the details can be skipped)

• ( )=bm

• ( ) =bm

1

0

1

1

nnk

k

rr

r

1

(1 )

1

nnk

k

r rr

r

1

0

1

1

ddk

dk

bt b

b

0

1

0

1

2

1

1

1 (1 )

1 (1 )

m

ii

im

i

m

t

b

b

m b b

b b

0

m

ii

t

- b is branching factor- td is the number of vertices visited for depth d

11

1

m

m

bt

b

mt

Page 50: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Conclusion

• The complexity of IDS is the same as DFS

Page 51: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Other Topics in Graph Theory

• Cut Vertices & Cut Edges• Euler Path/Circuit & Hamilton

Path/Circuit• Planarity

Page 52: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Cut Vertices & Cut Edges

• What is a cut vertex?– The removal of a set of vertices

causes a connected graph disconnected

• What is a cut edge?– The removal of a set of edges causes

a connected graph disconnected

Page 53: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Euler Path & Hamilton Path

• An Euler path is a path in a graph which visits each edge exactly once

• A Hamilton path is a path in an undirected graph which visits each vertex exactly once.

Page 54: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Planarity

• A planar graph is a graph that can be drawn so that no edges intersect

• K5 and K3,3 are non-planar graphs

Page 55: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Last Question:Equation

• Question: Find the number of solution of xi, given ki,pi. 1<=n<=6, 1<=xi<=150

• Vertex: possible values of – k1x1

p1 , k1x1p1 + k2x2

p2 , k1x1p1 + k2x2

p2 + k3x3p3 ,

k4x4p4 , k4x4

p4 + k5x5p5 , k4x4

p4 + k5x5p5 + k6x6

p6

Page 56: Graph Theory Kayman Lui 20-09-2007. Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.

Graph problems in Uva

• 280• 336• 532• 572• 10592