Supp Graph 1

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 40

Elementary

Elementary Graph
Graph Algorithms
Algorithms

Intro 1
Graphs
 G = (V, E)
» V = set of vertices
» E = set of edges  (VV)
 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 loops
are 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(V2)
aphs-1 - 2 Lin / Intro
Devi 2
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.

aphs-1 - 3 Lin / Intro


Devi 3
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 b a b d c

b c

d
If weighted, store weights also in
c d c
adjacency lists.
d

a b a b d c

b a c

c d c d a b

d a c

aphs-1 - 4 Lin / Intro


Devi 4
Adjacency Matrix
 |V|  |V| matrix A.
 Number vertices from 1 to |V| in some arbitrary manner.
 A is then given by: 1 if (i, j )  E
A[i, j ]  aij  
1
0 otherwise
2 1 2 3 4
a b
1 0 1 1 1
2 0 0 1 0
c d4 3 0 0 0 1
3 4 0 0 0 0
1 2 1 2 3 4
a b
1 0 1 1 1
2 1 0 1 0 A = AT for undirected graphs.
c d 3 1 1 0 1
3 4 4 1 0 1 0
aphs-1 - 5 Lin / Intro
Devi 5
Space and Time
 Space: (V2).
» Not efficient for large graphs.
 Time: to list all vertices adjacent to u: (V).
 Time: to determine if (u, v)  E: (1).
 Can store weights instead of bits for weighted graph.

aphs-1 - 6 Lin / Intro


Devi 6
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).

aphs-1 - 7 Lin / Intro


Devi 7
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] =  if v is not reachable from s.
 [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 reachable
vertices.
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 1 i  k-1. Error!
Length of the path: Number of edges in the path.
Path is simple if no vertex is repeated.
aphs-1 - 8 Lin / Intro
Devi 8
Breadth-first Search
 Expands the frontier between discovered and
undiscovered vertices uniformly across the breadth of the
frontier.
» A vertex is “discovered” the first time it is encountered during
the search.
» A vertex is “finished” if all vertices adjacent to it have been
discovered.
 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 be
implemented without colors.

aphs-1 - 9 Lin / Intro


Devi 9
BFS(G,s)
BFS(G,s)
1.1. for
foreach
eachvertex
vertexuuininV[G]
V[G]–– {s}{s}
22 do color[u]
docolor[u] white
white
33 d[u]
d[u] 
44 [u]
[u] nil
nil white: undiscovered
color[s]
55 color[s] gray
gray gray: discovered
black: finished
d[s]
66 d[s] 00
77 [s][s]
nil
nil
88 QQ  Q: a queue of discovered
99 enqueue(Q,s)
enqueue(Q,s) vertices
color[v]: color of v
10 whileQQ
10 while d[v]: distance from s to v
11
11 douu
do dequeue(Q)
dequeue(Q) [u]: predecessor of v
12
12 for
foreach
eachvvininAdj[u]
Adj[u]
13
13 do
doififcolor[v]
color[v]==white
white
14
14 then color[v]
thencolor[v] gray
gray Example: On board.
15
15 d[v]
d[v] d[u]
d[u]++11
16
16 [v]
[v]
uu
17
17 enqueue(Q,v)
enqueue(Q,v)
18
18 color[u]
color[u] black
black

aphs-1 - 11 Lin /Intro


Devi11
Example (BFS)
(Courtesy of Prof. Jim Anderson)

r s t u
 0  

   
v w x y

Q: s
0

aphs-1 - 12 Lin /Intro


Devi12
Example (BFS)

r s t u
1 0  

 1  
v w x y

Q: w r
1 1

aphs-1 - 13 Lin /Intro


Devi13
Example (BFS)

r s t u
1 0 2 

 1 2 
v w x y

Q: r t x
1 2 2

aphs-1 - 14 Lin /Intro


Devi14
Example (BFS)

r s t u
1 0 2 

2 1 2 
v w x y

Q: t x v
2 2 2

aphs-1 - 15 Lin /Intro


Devi15
Example (BFS)

r s t u
1 0 2 3

2 1 2 
v w x y

Q: x v u
2 2 3

aphs-1 - 16 Lin /Intro


Devi16
Example (BFS)

r s t u
1 0 2 3

2 1 2 3
v w x y

Q: v u y
2 3 3

aphs-1 - 17 Lin /Intro


Devi17
Example (BFS)

r s t u
1 0 2 3

2 1 2 3
v w x y

Q: u y
3 3

aphs-1 - 18 Lin /Intro


Devi18
Example (BFS)

r s t u
1 0 2 3

2 1 2 3
v w x y

Q: y
3

aphs-1 - 19 Lin /Intro


Devi19
Example (BFS)

r s t u
1 0 2 3

2 1 2 3
v w x y

Q: 

aphs-1 - 20 Lin /Intro


Devi20
Example (BFS)

r s t u
1 0 2 3

2 1 2 3
v w x y

BF Tree

aphs-1 - 21 Lin /Intro


Devi21
Depth-first Search (DFS)
 Explore edges out of the most recently discovered
vertex v.
 When all edges of v have been explored, backtrack to
explore other edges leaving the vertex from which v
was discovered (its predecessor).
 “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.
aphs-1 - 22 Lin /Intro
Devi22
Depth-first Search
 Input: G = (V, E), directed or undirected. No source
vertex given!
 Output:
» 2 timestamps on each vertex. Integers between 1 and 2|V|.
• d[v] = discovery time (v turns from white to gray)
• f [v] = finishing time (v turns from gray to black)
 [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.

aphs-1 - 23 Lin /Intro


Devi23
Depth-First Trees
 Predecessor subgraph defined slightly different from
that of BFS.
 The predecessor subgraph of DFS is G = (V, E) where
E ={([v],v) : v  V and [v]  NIL}.
» How does it differ from that of BFS?
» The predecessor subgraph G forms a depth-first forest
composed of several depth-first trees. The edges in E are
called tree edges.

Definition:
Forest: An acylclic graph G that may be disconnected.

aphs-1 - 24 Lin /Intro


Devi24
Pseudo-code
DFS(G)
DFS(G) DFS-Visit(u)
DFS-Visit(u)
1.1. for
foreach vertexuuV[G]
eachvertex V[G] color[u]
1.1. color[u] GRAY White
GRAY Whitevertex
vertexuu
has
hasbeen
beendiscovered
2.2. do color[u]
docolor[u] white
white discovered
3.3. [u]
[u] NIL
NIL
 time
time time
time++11
time
4.4. time 00
1.1. d[u]
d[u] timetime
5.5. for
foreach vertexuuV[G]
eachvertex V[G]
2.2. for eachvvAdj[u]
foreach Adj[u]
6.6. do 3.3. do
doififcolor[v]
color[v]==WHITE
doififcolor[u]
color[u]==white
white WHITE
7.7. then
thenDFS-Visit(u)
DFS-Visit(u)
4.4. then[v]
then [v]uu
5.5. DFS-Visit(v)
DFS-Visit(v)
6.6. color[u]
color[u] BLACK Blacken
BLACK Blackenu;u;
Uses a global timestamp time. ititisisfinished.
finished.
 f[u]
f[u] time
time time
time++11
Example: On board.

aphs-1 - 25 Lin /Intro


Devi25
Example (DFS)
(Courtesy of Prof. Jim Anderson)

u v w
1/

x y z

aphs-1 - 26 Lin /Intro


Devi26
Example (DFS)

u v w
1/ 2/

x y z

aphs-1 - 27 Lin /Intro


Devi27
Example (DFS)

u v w
1/ 2/

3/
x y z

aphs-1 - 28 Lin /Intro


Devi28
Example (DFS)

u v w
1/ 2/

4/ 3/
x y z

aphs-1 - 29 Lin /Intro


Devi29
Example (DFS)

u v w
1/ 2/
B

4/ 3/
x y z

aphs-1 - 30 Lin /Intro


Devi30
Example (DFS)

u v w
1/ 2/
B

4/5 3/
x y z

aphs-1 - 31 Lin /Intro


Devi31
Example (DFS)

u v w
1/ 2/
B

4/5 3/6
x y z

aphs-1 - 32 Lin /Intro


Devi32
Example (DFS)

u v w
1/ 2/7
B

4/5 3/6
x y z

aphs-1 - 33 Lin /Intro


Devi33
Example (DFS)

u v w
1/ 2/7

F B

4/5 3/6
x y z

aphs-1 - 34 Lin /Intro


Devi34
Example (DFS)

u v w
1/8 2/7

F B

4/5 3/6
x y z

aphs-1 - 35 Lin /Intro


Devi35
Example (DFS)

u v w
1/8 2/7 9/

F B

4/5 3/6
x y z

aphs-1 - 36 Lin /Intro


Devi36
Example (DFS)

u v w
1/8 2/7 9/

F B C

4/5 3/6
x y z

aphs-1 - 37 Lin /Intro


Devi37
Example (DFS)

u v w
1/8 2/7 9/

F B C

4/5 3/6 10/


x y z

aphs-1 - 38 Lin /Intro


Devi38
Example (DFS)

u v w
1/8 2/7 9/

F B C

4/5 3/6 10/ B


x y z

aphs-1 - 39 Lin /Intro


Devi39
Example (DFS)

u v w
1/8 2/7 9/

F B C

4/5 3/6 10/11 B


x y z

aphs-1 - 40 Lin /Intro


Devi40
Example (DFS)

u v w
1/8 2/7 9/12

F B C

4/5 3/6 10/11 B


x y z

aphs-1 - 41 Lin /Intro


Devi41

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy