41
Intro 1  Elementary Graph Algorithms

Supp Graph 1

Embed Size (px)

Citation preview

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 1/41

Intro 1

 Elementary Graph Algorithms

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 2/41

graphs-1 - 2 Lin / DeviIntro 2

Graphs

G =(V 

, E 

)» V = set of vertices

» E = set of edges (V vV )

Types of graphs

» Undirected: edge (u, v)=

(v, u); for all v, (v, v)  E 

( No self loops.)

» Directed: (u, v) is edge from u to v, denoted as u v. Self loopsare allowed.

» Weighted: each edge has an associated weight, given by a weight

function w :  E  R .» Dense: | E | } |V |2.

» Sparse: | E | << |V |2.

| E | = O(V 2)

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 3/41

graphs-1 - 3 Lin / DeviIntro 3

Representation of Graphs

Two standard ways.» Adjacency Lists.

» Adjacency Matrix.

If (u, v)  E , then vertex v is adjacent to vertex u.

Adjacency relationship is:

» Symmetric if G is undirected.

» Not necessarily so if G is directed.

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 4/41

graphs-1 - 4 Lin / DeviIntro 4

Adjacency Lists

Consists of an array  Adj of |V | lists. One list per vertex.

For u V ,  Adj[u] consists of all vertices adjacent to u.

a

dc

b a

b

c

d

 b

c

d

d c

a

dc

b a

b

c

d

 b

a

d

d c

c

a b

a c

If weighted, store weights also in

adjacency lists.

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 5/41

graphs-1 - 5 Lin / DeviIntro 5

Adjacency Matrix

|V | v |V | matrix A.  Number vertices from 1 to |V | in some arbitrary manner.

A is then given by:

°¯®

!!other ise0

),(i1],[

 E  jia ji ij

a

dc

b

a

dc

b

1 2

3 4

1 2

3 4

1 2 3 41 0 1 1 1

2 0 0 1 0

3 0 0 0 1

4 0 0 0 0

1 2 3 4

1 0 1 1 1

2 1 0 1 0

3 1 1 0 1

4 1 0 1 0

 A =  AT for undirected graphs.

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 6/41

graphs-1 - 6 Lin / DeviIntro 6

Space and Time

Space: 5

(V 2

).» Not efficient for large graphs.

Time: to list all vertices adjacent to u: 5(V ).

Time: to determine if (u, v ) E : 5(1).

Can store weights instead of bits for weighted graph.

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 7/41

graphs-1 - 7 Lin / DeviIntro 7

Graph-searching Algorithms

Searching a graph:» Systematically following the edges of a graph so as to visit the

vertices of the graph.

Used to discover the structure of a graph.

Standard graph-searching algorithms.» Breadth-first Search (BFS).

» Depth-first Search (DFS).

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 8/41

graphs-1 - 8 Lin / DeviIntro 8

Breadth-first Search

Input: Graph G = (V ,  E ), either directed or undirected,and source vertex  s  V .

Output:

» d [v] = distance (smallest # of edges, or shortest path) from  s to v,for all v V . d [v] = g if v is not reachable from  s.

» T [v] = u such that (u, v) is last edge on shortest path  s v. u is v¶s predecessor.

» Builds breadth-first tree with root  s that contains all reachablevertices.

Definitions:

Path between vertices u and v: Sequence of vertices (v1, v2, «, vk ) such that

u=v1 and v =vk , and (vi,vi+1)  E , for all 1e i e k -1.

Length of the path: Number of edges in the path.

Path is simple if no vertex is repeated.

Error!

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 9/41

graphs-1 - 9 Lin / DeviIntro 9

Breadth-first Search

Expands the frontier between discovered andundiscovered vertices uniformly across the breadth of thefrontier.

» A vertex is ³discovered´ the first time it is encountered duringthe search.

» A vertex is ³finished´ if all vertices adjacent to it have beendiscovered.

Colors the vertices to keep track of progress.

» White ± Undiscovered.

» Gray ± Discovered but not finished.» Black ± Finished.

Colors are required only to reason about the algorithm. Can beimplemented without colors.

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 10/41

graphs-1 - 10 Lin / DeviIntro 10

BFS for Shortest Paths

Finished Discovered Undiscovered

 S 

11

1 S 2

2

2

2

2

2

 S 

3

3 3

3

3

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 11/41

graphs-1 - 11 Lin / DeviIntro 11

 

BFS(G,s)

1. f or each vertex u in V[G] ± {s}

2 do color [u]n white

3 d [u]n w

4 T [u] n nil

5 color[ s]n gray

6 d[ s]n 0

7 T [ s] n nil

8 Qn*

9 enqueue(Q,s)10 whileQ { *

11 do u n dequeue(Q)

12 f or each v in Adj[u]

13 do if color[v] = white

14 then color[v]n gray

15 d [v] n d [u] + 1

16 T [v] n u

17 enqueue(Q,v)

18 color[u]n black 

white: undiscovered

gray: discovered

 black: finished

Q: a queue of discovered

verticescolor[v]: color of v

d[v]: distance from s to v

T [u]: predecessor of v

Example: On board.

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 12/41

graphs-1 - 12 Lin / DeviIntro 12

Example (BFS)

g 0

g g g

g g

g

r  s t u

vw x y

Q: s

0

(Courtesy of Prof. Jim Anderson)

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 13/41

graphs-1 - 13 Lin / DeviIntro 13

Example (BFS)

1 0

1 g g

g g

g

r  s t u

v wx y

Q: w r 

1 1

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 14/41

graphs-1 - 14 Lin / DeviIntro 14

Example (BFS)

1 0

1 2 g

2 g

g

r  s t u

v wx y

Q: r t x

1 2 2

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 15/41

graphs-1 - 15 Lin / DeviIntro 15

Example (BFS)

1 0

1 2 g

2 g

2

r  s t u

v wx

y

Q: t x v

2 2 2

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 16/41

graphs-1 - 16 Lin / DeviIntro 16

Example (BFS)

1 0

1 2 g

2 3

2

r  s t u

v wx

y

Q: x v u

2 2 3

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 17/41

graphs-1 - 17 Lin / DeviIntro 17

Example (BFS)

1 0

1 2 3

2 3

2

r  s t u

v w x y

Q: v u y

2 3 3

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 18/41

graphs-1 - 18 Lin / DeviIntro 18

Example (BFS)

1 0

1 2 3

2 3

2

r  s t u

v w x y

Q: u y

3 3

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 19/41

graphs-1 - 19 Lin / DeviIntro 19

Example (BFS)

1 0

1 2 3

2 3

2

r  s t u

v w x y

Q: y

3

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 20/41

graphs-1 - 20 Lin / DeviIntro 20

Example (BFS)

1 0

1 2 3

2 3

2

r  s t u

v w x y

Q:

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 21/41

graphs-1 - 21 Lin / DeviIntro 21

Example (BFS)

1 0

1 2 3

2 3

2

r  s t u

v w x y

BFTree

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 22/41

graphs-1 - 22 Lin / DeviIntro 22

Depth-first Search (DFS)

Explore edges out of the most recently discoveredvertex v.

When all edges of v have been explored, backtrack to

explore other edges leaving the vertex from which v

was discovered (its pred ece ssor ). ³Search as deep as possible first.´

Continue until all vertices reachable from the original

source are discovered.

If any undiscovered vertices remain, then one of them

is chosen as a new source and search is repeated from

that source.

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 23/41

graphs-1 - 23 Lin / DeviIntro 23

Depth-first Search

In

put: G

 =

(V ,E ), directed or undirected. No sourcevertex given!

Output:

» 2 timestamps on each vertex. Integers between 1 and 2|V|.

d [v]=

di scovery t ime (v turns from white to gray)  f [v] =  f ini shing t ime (v turns from gray to black)

» T [v] : predecessor of v = u, such that v was discovered during

the scan of u¶s adjacency list.

Uses the same coloring scheme for vertices as BFS.

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 24/41

graphs-1 - 24 Lin / DeviIntro 24

Depth-First Trees

Predecessor subgraph defined slightly different fromthat of BFS.

The predecessor subgraph of DFS is GT = (V,  E 

T )

where  E T ={(T [v],v) : v V and T [v] { NIL}.

» How does it differ from that of BFS?» The predecessor subgraphG

T forms a d e pth-f ir  st f ore st 

composed of several d e pth-f ir  st t ree s. The edges in  E T 

are

called t ree edg e s.

Definition:

Forest: An acylclic graph G that may be disconnected.

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 25/41

graphs-1 - 25 Lin / DeviIntro 25

Pseudo-code

 

DFS(G )

1. f or each vertex u V [G]

2. do color [u] n white

3. T [u] n NIL

4. t ime n 0

5. f or each vertex u V [G]6. do if color [u] = white

7. then DFS-Visit(u)

Uses a global timestamp t ime.

 

DFS-Visit(u)

1. color [u]n GRAY White vertex u

has been discovered

2. t imen t ime + 1

3. d [u] n t ime

4.f or

each v  Adj[u]5. do if color [v] = WHITE

6. then T [v] n u

7. DFS-Visit(v)

8. color [u] n BLACK  Blacken u;

it is finished.9.  f [u] n t ime n t ime + 1

Example: On board.

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 26/41

graphs-1 - 26 Lin / DeviIntro 26

Example (DFS)

1/

u v w

x y z

(Courtesy of Prof. Jim Anderson)

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 27/41

graphs-1 - 27 Lin / DeviIntro 27

Example (DFS)

1/ 2/

u v w

x y z

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 28/41

graphs-1 - 28 Lin / DeviIntro 28

Example (DFS)

1/

3/

2/

u v w

x y z

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 29/41

graphs-1 - 29 Lin / DeviIntro 29

Example (DFS)

1/

4/ 3/

2/

u v w

x y z

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 30/41

graphs-1 - 30 Lin / DeviIntro 30

Example (DFS)

1/

4/ 3/

2/

u v w

x y z

B

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 31/41

graphs-1 - 31 Lin / DeviIntro 31

Example (DFS)

1/

4/5 3/

2/

u v w

x y z

B

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 32/41

graphs-1 - 32 Lin / DeviIntro 32

Example (DFS)

1/

4/5 3/6

2/

u v w

x y z

B

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 33/41

graphs-1 - 33 Lin / DeviIntro 33

Example (DFS)

1/

4/5 3/6

2/7

u v w

x y z

B

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 34/41

graphs-1 - 34 Lin / DeviIntro 34

Example (DFS)

1/

4/5 3/6

2/7

u v w

x y z

BF

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 35/41

graphs-1 - 35 Lin / DeviIntro 35

Example (DFS)

1/8

4/5 3/6

2/7

u v w

x y z

BF

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 36/41

graphs-1 - 36 Lin / DeviIntro 36

Example (DFS)

1/8

4/5 3/6

2/7 9/

u v w

x y z

BF

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 37/41

graphs-1 - 37 Lin / DeviIntro 37

Example (DFS)

1/8

4/5 3/6

2/7 9/

u v w

x y z

BF C

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 38/41

graphs-1 - 38 Lin / DeviIntro 38

Example (DFS)

1/8

4/5 3/6 10/

2/7 9/

u v w

x y z

BF C

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 39/41

graphs-1 - 39 Lin / DeviIntro 39

Example (DFS)

1/8

4/5 3/6 10/

2/7 9/

u v w

x y z

BF C

B

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 40/41

graphs-1 - 40 Lin / DeviIntro 40

Example (DFS)

1/8

4/5 3/6 10/11

2/7 9/

u v w

x y z

BF C

B

8/8/2019 Supp Graph 1

http://slidepdf.com/reader/full/supp-graph-1 41/41

graphs-1 - 41 Lin / DeviIntro 41

Example (DFS)

1/8

4/5 3/6 10/11

2/7 9/12

u v w

x y z

BF C

B