advanced data structures lecture 2
TRANSCRIPT
-
8/14/2019 Advanced Data Structures lecture 2
1/69
-
8/14/2019 Advanced Data Structures lecture 2
2/69
Introductory remarks
What is a graph?
A data structure consisting of a finite set of nodes and
edges between nodes. Edges can be directed (in directed
graphs) or not (in undirected graphs).
What can be represented as a graph?1 physical networks: electrical circuits, roadways, organic
molecules , etc.2 interactions in ecosystems, social relationships, databases3 program flow of control4 etc.
Data Structures for Graphs
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
3/69
-
8/14/2019 Advanced Data Structures lecture 2
4/69
Graphs: Applications (continued)
4. Study the structure and density of the Internet computer network
5. Check network connectivity: are 2 computers connected by acommunication link?
Data Structures for Graphs
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
5/69
Graphs: Applications (continued)
6. Find shortest paths between 2 cities in a transportation
network
Approach: weighted graphs
7. Schedule exams
8. assign channels to TV stations
Data Structures for Graphs
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
6/69
Types of graphsDirected graph (digraph)
G= (V, E)where
V =finite set of vertices (or nodes)E=set of arcs (or directed edges) between nodes.Every arc is from asource(or tail) node to adestination(or head) node.
A digraph can be
simple: there is at most one arc between two nodes. An arc from a nodeuto
a nodevis represented as u vand drawn as u v
Ifu vis an arc then we say v isadjacent tou.
multiple: there can be more than one arc from one node to another.Distinct arcs with same source and destination can be distinguishedbylabelingthem. An arc with labelefromutovis represented
u e vand drawn as
u ve
path= sequence of nodes =v1, . . . , vnsuch thatv1 v2,. . . ,vn1 vnarearcs.Thelengthof isn 1. is a pathfromv1 tovn.
simple path= path with distinct nodes, except possibly the first and last.
simple cycle= path of length at least 1 that begins and ends at the same node.
Data Structures for Graphs
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
7/69
Types of graphsDigraphs (continued.)
Example ofsimple digraph: communication network with one-way phone lines:
Example ofmultiple digraph: communication network with multiple one-wayphone lines:
Data Structures for Graphs
T f h
http://find/http://goback/ -
8/14/2019 Advanced Data Structures lecture 2
8/69
Types of graphsDigraphs (continued.)
Example of simple digraph:
1 2
3 4
1,2,4 is a path of length 2 from node 1 to node 4.
3,2,4,3 is a cycle of length 3.
Example of labeled digraph: a transition digraph
1 2
4 3
a
a
a
a
b b b b
Data Structures for Graphs
R t ti f di h
http://goforward/http://find/http://goback/ -
8/14/2019 Advanced Data Structures lecture 2
9/69
Representations for digraphs
G= (V, E)withV ={1, . . . , n}
Adjacency matrixA=
A[1][1] A[1][2] A[1][n]A[2][1] A[2][2] A[2][n]
... ...
. . . ...
A[n][1] A[n][2] A[n][n]
whereA[i][j] =
1 ifijis an arc0 otherwise
Labeled adjacency matrixAwhere
For simple labeled digraph: A[i][j] = e ifi ejE
blank otherwise. For multiple labeled digraph: A[i][j]can be the list of labels
of arcs from nodeito nodej.
Data Structures for Graphs
R t ti f di h
http://goforward/http://find/http://goback/ -
8/14/2019 Advanced Data Structures lecture 2
10/69
Representations for digraphsAdjacency matrix. Example. Advantages and disadvantages
1 2
4 3
a
a
a
a
b b b b
1 2 3 4
1 a b2 a b
3 b a
4 b a
1 2
3 4
1 2 3 4
1 1 1
2 1
3 1
4 1Advantages: O(1)time to check if there is an arc ij.
Disadvantages: (n2)storage even if the digraph hasn2
arcs.
Data Structures for Graphs
R t ti f di h
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
11/69
Representations of digraphsAdjacency list
G= (V, E)whereV ={1, . . . , n}Adjacency list= array of pointersHead[1..n]where
Head[i] =pointer to the list of nodes adjacent to i.
Alternative representation: two arrays of integers
Head[1..n]and Adj[1..m]such that, for every 1 in:
Adj[Head[i]], Adj[Head[i] +1], . . .contain the nodes adjacent toi, up to that point where we first encounter 0, which marks the
end of the list of nodes adjacent to i.
Data Structures for Graphs
Representations for digraphs
http://goforward/http://find/http://goback/ -
8/14/2019 Advanced Data Structures lecture 2
12/69
Representations for digraphsAdjacency lists: Example
Example
1 2
3 4
Data Structures for Graphs
Representations for digraphs
http://goforward/http://find/http://goback/ -
8/14/2019 Advanced Data Structures lecture 2
13/69
Representations for digraphsList of all edges
IfG= (V,
E)is a simple digraph with small number of arcs,thenGcan be represented by the list of pairs (v, w)such thatvwis an arc inE.
Vcan be retrieved by traversing the list of edges.
Example
1 2
3 4
EdgeList ={(1, 2),(1, 3),(2, 4),(3, 2),(4, 3)}.
Data Structures for Graphs
http://goforward/http://find/http://goback/ -
8/14/2019 Advanced Data Structures lecture 2
14/69
Connectivity in digraphs
-
8/14/2019 Advanced Data Structures lecture 2
15/69
Connectivity in digraphs
Assumption:G= (V, E)is digraph withV ={1, 2, . . . , n}
Given: two nodesi,jV
Determine: whether there exists a path fromi toj.
Thetransitive closureof Gis the graphG = (V, E)such that
(v, v) E if and only if there exists a path fromv tov inG.
Data Structures for Graphs
Connectivity in digraphs
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
16/69
Connectivity in digraphs
Assumption:G= (V, E)is digraph withV ={1, 2, . . . , n}
Given: two nodesi,jV
Determine: whether there exists a path fromi toj.
Thetransitive closureof Gis the graphG = (V, E)such that
(v, v) E if and only if there exists a path fromv tov inG.
LetA and A be the adjacency matrices ofGandG.
Aand A are matrices of sizen nwhose elements are 0 and 1.Such matrices are called Boolean matrices. (0=false, 1=true)
Data Structures for Graphs
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
17/69
-
8/14/2019 Advanced Data Structures lecture 2
18/69
Transitive Closure
-
8/14/2019 Advanced Data Structures lecture 2
19/69
Transitive ClosureHow to computeA?
IfA, Bare Boolean matrices of size n n, we can define:
A B=C ifC[i][j] =A[i][j] B[i][j] =max(A[i][j], B[i][j])
( is Boolean addition) A B=C ifC[i][j] =A[i][1] B[1][j] . . . A[i][n] B[n][j].
( is the Boolean product defined byb1 b2 :=min(b1, b2).)
Data Structures for Graphs
Transitive Closure
http://find/http://goback/ -
8/14/2019 Advanced Data Structures lecture 2
20/69
Transitive ClosureHow to computeA?
IfA, Bare Boolean matrices of size n n, we can define:
A B=C ifC[i][j] =A[i][j] B[i][j] =max(A[i][j], B[i][j])
( is Boolean addition) A B=C ifC[i][j] =A[i][1] B[1][j] . . . A[i][n] B[n][j].
( is the Boolean product defined byb1 b2 :=min(b1, b2).)
There exists a path of lengthpfromi tojiff the element at position(i,j)of the
matrixAp
=A . . . A p+1 times
is 1, whereA is the adjacency matrix of G.
Data Structures for Graphs
Transitive Closure
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
21/69
Transitive ClosureHow to computeA?
IfA, Bare Boolean matrices of size n n, we can define:
A B=C ifC[i][j] =A[i][j] B[i][j] =max(A[i][j], B[i][j])
( is Boolean addition) A B=C ifC[i][j] =A[i][1] B[1][j] . . . A[i][n] B[n][j].
( is the Boolean product defined byb1 b2 :=min(b1, b2).)
There exists a path of lengthpfromi tojiff the element at position(i,j)of the
matrixAp
=A . . . A p+1 times
is 1, whereA is the adjacency matrix of G.
There exists a path of length pfromi tojiff the element at position(i,j)of thematrixId A . . . Ap+1 is 1, whereA is the adjacency matrix of G, andId isthe identity matrix.
Data Structures for Graphs
Transitive Closure
http://goforward/http://find/http://goback/ -
8/14/2019 Advanced Data Structures lecture 2
22/69
Transitive ClosureHow to computeA?
IfA, Bare Boolean matrices of size n n, we can define:
A B=C ifC[i][j] =A[i][j] B[i][j] =max(A[i][j], B[i][j])
( is Boolean addition) A B=C ifC[i][j] =A[i][1] B[1][j] . . . A[i][n] B[n][j].
( is the Boolean product defined byb1 b2 :=min(b1, b2).)
There exists a path of lengthpfromi tojiff the element at position(i,j)of the
matrixAp
=A . . . A p+1 times
is 1, whereA is the adjacency matrix of G.
There exists a path of length pfromi tojiff the element at position(i,j)of thematrixId A . . . Ap+1 is 1, whereA is the adjacency matrix of G, andId isthe identity matrix.
If there is a path fromi toj inGthen there is a path of length n 1 fromi toj.
Data Structures for Graphs
Transitive Closure
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
23/69
How to computeA?
IfA, Bare Boolean matrices of size n n, we can define:
A B=C ifC[i][j] =A[i][j] B[i][j] =max(A[i][j], B[i][j])
( is Boolean addition) A B=C ifC[i][j] =A[i][1] B[1][j] . . . A[i][n] B[n][j].
( is the Boolean product defined byb1 b2 :=min(b1, b2).)
There exists a path of lengthpfromi tojiff the element at position(i,j)of the
matrixAp
=A . . . A p+1 times
is 1, whereA is the adjacency matrix of G.
There exists a path of length pfromi tojiff the element at position(i,j)of thematrixId A . . . Ap+1 is 1, whereA is the adjacency matrix of G, andId isthe identity matrix.
If there is a path fromi toj inGthen there is a path of length n 1 fromi toj.
The transitive closure of adjacency matrixA is the matrixA whereA[i][j] =1
iff there exists a path of length 1 fromi toj.
Data Structures for Graphs
Transitive Closure
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
24/69
How to computeA?
IfA, Bare Boolean matrices of size n n, we can define:
A B=C ifC[i][j] =A[i][j] B[i][j] =max(A[i][j], B[i][j])
( is Boolean addition) A B=C ifC[i][j] =A[i][1] B[1][j] . . . A[i][n] B[n][j].
( is the Boolean product defined byb1 b2 :=min(b1, b2).)
There exists a path of lengthpfromi tojiff the element at position(i,j)of the
matrixA
p
=A . . . A p+1 times
is 1, whereA is the adjacency matrix of G.
There exists a path of length pfromi tojiff the element at position(i,j)of thematrixId A . . . Ap+1 is 1, whereA is the adjacency matrix of G, andId isthe identity matrix.
If there is a path fromi toj inGthen there is a path of length n 1 fromi toj.
The transitive closure of adjacency matrixA is the matrixA whereA[i][j] =1
iff there exists a path of length 1 fromi toj.
A =Id+A . . . An (Why?)
Data Structures for Graphs
Transitive Closure
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
25/69
Warshalls algorithm
The previous approach to computeA has time complexityO(n4). (Why?)
Too expensive!
Warshall discovered a much better approach:
For alli,j V = {1, 2, . . . , n} consider all paths fromi toj whose
intermediate nodes are from {1, . . . , k}.Let C[i][j](k) be 1 if there exists
such a path, and 0 otherwise.Warshall observed thatC[i][j](k) can be computed by recursion on k:
C[i][j](k) =
A[i][j] ifk=0,C[i][j](k1) C[i][k](k1) C[k][j](k1) ifk 1.
i,jare connected by a path iff C[i][j](n) =1.
Data Structures for Graphs
Transitive closure
http://find/http://goback/ -
8/14/2019 Advanced Data Structures lecture 2
26/69
Warshalls algorithm
procedure Warshall
input: int A[n][n] // an n x n Boolean matrix
output: int C[n][n] // the transitive closure of A
for (int i=1; i < n; i++)
for (int j=1;j
-
8/14/2019 Advanced Data Structures lecture 2
27/69
g p
Given a digraph(V, E), anda distinguished source nodesV
Visit all nodes that are reachable from s.Breadth-first traversalsolves this problem as follows:
It produces abreadth-first treewith roots, such that the childrenof every nodenare the nodes reachable from n, which are notyet in the tree.
Data Structures for Graphs
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
28/69
Traversal of directed graphs: Breadth-first traversal
-
8/14/2019 Advanced Data Structures lecture 2
29/69
Given a digraph(V, E), anda distinguished source nodesV
Visit all nodes that are reachable from s.Breadth-first traversalsolves this problem as follows:
It produces abreadth-first treewith roots, such that the childrenof every nodenare the nodes reachable from n, which are notyet in the tree.
Example
sr
wv
t
x
u
y
Data Structures for Graphs
Traversal of directed graphs: Breadth-first traversal
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
30/69
Given a digraph(V, E), anda distinguished source nodesV
Visit all nodes that are reachable from s.Breadth-first traversalsolves this problem as follows:
It produces abreadth-first treewith roots, such that the childrenof every nodenare the nodes reachable from n, which are notyet in the tree.
Example
sr
wv
t
x
u
y
sLevel 0:
Data Structures for Graphs
Traversal of directed graphs: Breadth-first traversal
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
31/69
Given a digraph(V, E), anda distinguished source nodesV
Visit all nodes that are reachable from s.Breadth-first traversalsolves this problem as follows:
It produces abreadth-first treewith roots, such that the childrenof every nodenare the nodes reachable from n, which are notyet in the tree.
Example
sr
w
r
wv
t
x
u
y
sLevel 0:
Level 1: r w
1 2
Data Structures for Graphs
Traversal of directed graphs: Breadth-first traversal
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
32/69
Given a digraph(V, E), anda distinguished source nodesV
Visit all nodes that are reachable from s.Breadth-first traversalsolves this problem as follows:
It produces abreadth-first treewith roots, such that the childrenof every nodenare the nodes reachable from n, which are notyet in the tree.
Example
sr
wv
t
x
u
y
sLevel 0:
Level 1: r w
1 2
Level 2: v t x
3 4 5
Data Structures for Graphs
Traversal of directed graphs: Breadth-first traversal
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
33/69
Given a digraph(V, E), anda distinguished source nodesV
Visit all nodes that are reachable from s.Breadth-first traversalsolves this problem as follows:
It produces abreadth-first treewith roots, such that the childrenof every nodenare the nodes reachable from n, which are notyet in the tree.
Example
sr
wv
t
x
u
y
sLevel 0:
Level 1: r w
1 2
Level 2: v t x
3 4 5
Level 3: u y
6 7
Data Structures for Graphs
Properties of the breadth-first tree
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
34/69
1 It contains all vertices reachable froms.2 The path fromstovin the tree corresponds tothe shortest
pathfromstovin the graph.
3 Q: Why is it named "breadth-first tree"?
A: It expands the frontier between discovered andundiscovered nodes uniformly across the breadth of the
frontier.4 The breadth-first search distinguishes 2 kinds of nodes:
1 undiscovered (white)2 discovered (black)
Data Structures for Graphs
How to implement breadth-first traversal?
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
35/69
(1) In each expansion step, the algorithm looks up the
neighbors of the leaves of the tree
representation of graph with adjacency lists isconvenient.
Example (Representation with adjacency lists)
sr
wv
t
x
u
y
r [v],
v [],
s [r, w], w [t, x],t [u, x], x [y],u [], y [u].
Possible implementation in C++:
typedef string node; // if nodes are identified by strings
typedef list adjList;
map graph;
Data Structures for Graphs
How to implement breadth-first traversal?
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
36/69
Data Structures for Graphs
How to implement breadth-first traversal?
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
37/69
(2) Expand the leaves of the breadth-first tree in the order in whichthey show up keep the leaves of the tree in a queue(it can be a queuecontainer in C++)
Data Structures for Graphs
How to implement breadth-first traversal?
http://find/http://goback/ -
8/14/2019 Advanced Data Structures lecture 2
38/69
(2) Expand the leaves of the breadth-first tree in the order in whichthey show up keep the leaves of the tree in a queue(it can be a queuecontainer in C++)
(3) Other desirable features:
Compute the depths of nodes in the breadth-first searchtree (=shortest distance from rootsto that node)Compute the parent of each node in the breadth-first
search tree.
r
v t
s
w
x
u y
Data Structures for Graphs
How to implement breadth-first traversal?
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
39/69
(2) Expand the leaves of the breadth-first tree in the order in whichthey show up keep the leaves of the tree in a queue(it can be a queuecontainer in C++)
(3) Other desirable features:
Compute the depths of nodes in the breadth-first searchtree (=shortest distance from rootsto that node)Compute the parent of each node in the breadth-first
search tree.
r
v t
s
w
x
u y
s
nodeColor[s]=BLACKparent[s]=null
rootDistance[s]=0
leaves=[s]
Data Structures for Graphs
How to implement breadth-first traversal?
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
40/69
(2) Expand the leaves of the breadth-first tree in the order in whichthey show up keep the leaves of the tree in a queue(it can be a queuecontainer in C++)
(3) Other desirable features:
Compute the depths of nodes in the breadth-first searchtree (=shortest distance from rootsto that node)Compute the parent of each node in the breadth-first
search tree.
r
v t
s
w
x
u y
s
r
1
nodeColor[r]=BLACKparent[r]=s
rootDistance[r]=1
leaves=[r]
Data Structures for Graphs
How to implement breadth-first traversal?
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
41/69
(2) Expand the leaves of the breadth-first tree in the order in whichthey show up keep the leaves of the tree in a queue(it can be a queuecontainer in C++)
(3) Other desirable features:
Compute the depths of nodes in the breadth-first searchtree (=shortest distance from rootsto that node)Compute the parent of each node in the breadth-first
search tree.
r
v t
s
w
x
u y
1
s
r
2
w
nodeColor[w]=BLACKparent[w]=s
rootDistance[w]=1
leaves=[r,w]
Data Structures for Graphs
How to implement breadth-first traversal?
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
42/69
(2) Expand the leaves of the breadth-first tree in the order in whichthey show up keep the leaves of the tree in a queue(it can be a queuecontainer in C++)
(3) Other desirable features:
Compute the depths of nodes in the breadth-first searchtree (=shortest distance from rootsto that node)Compute the parent of each node in the breadth-firstsearch tree.
r
v t
s
w
x
u y
1
s
r
2
w
3
vnodeColor[v]=BLACKparent[v]=r
rootDistance[v]=2
leaves=[w,v]
Data Structures for Graphs
How to implement breadth-first traversal?
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
43/69
(2) Expand the leaves of the breadth-first tree in the order in whichthey show up keep the leaves of the tree in a queue(it can be a queuecontainer in C++)
(3) Other desirable features:
Compute the depths of nodes in the breadth-first searchtree (=shortest distance from rootsto that node)Compute the parent of each node in the breadth-firstsearch tree.
r
v t
s
w
x
u y
1
s
r
2
w
3
v t4
nodeColor[t]=BLACK
parent[t]=wrootDistance[t]=2
leaves=[v,t]
Data Structures for Graphs
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
44/69
How to implement breadth-first traversal?
-
8/14/2019 Advanced Data Structures lecture 2
45/69
(2) Expand the leaves of the breadth-first tree in the order in whichthey show up keep the leaves of the tree in a queue(it can be a queuecontainer in C++)
(3) Other desirable features:
Compute the depths of nodes in the breadth-first searchtree (=shortest distance from rootsto that node)Compute the parent of each node in the breadth-firstsearch tree.
r
v t
s
w
x
u y
1
s
r
2
w
3
v4
t5
x
leaves=[t,x]
Data Structures for Graphs
How to implement breadth-first traversal?
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
46/69
(2) Expand the leaves of the breadth-first tree in the order in whichthey show up keep the leaves of the tree in a queue(it can be a queuecontainer in C++)
(3) Other desirable features:
Compute the depths of nodes in the breadth-first searchtree (=shortest distance from rootsto that node)Compute the parent of each node in the breadth-firstsearch tree.
r
v t
s
w
x
u y
1
s
r
2
w
3
v4
t5
x
6
u
nodeColor[u]=BLACKparent[u]=t
rootDistance[u]=3
leaves=[x,u]
Data Structures for Graphs
How to implement breadth-first traversal?
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
47/69
(2) Expand the leaves of the breadth-first tree in the order in whichthey show up keep the leaves of the tree in a queue(it can be a queuecontainer in C++)
(3) Other desirable features:
Compute the depths of nodes in the breadth-first searchtree (=shortest distance from rootsto that node)Compute the parent of each node in the breadth-firstsearch tree.
r
v t
s
w
x
u y
1
s
r
2
w
3
v4
t5
x
6
u
7
y
nodeColor[y]=BLACKparent[y]=x
rootDistance[y]=3
leaves=[u,y]
Data Structures for Graphs
How to implement breadth-first traversal?
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
48/69
(2) Expand the leaves of the breadth-first tree in the order in whichthey show up keep the leaves of the tree in a queue(it can be a queuecontainer in C++)
(3) Other desirable features:
Compute the depths of nodes in the breadth-first searchtree (=shortest distance from rootsto that node)Compute the parent of each node in the breadth-firstsearch tree.
r
v t
s
w
x
u y
1
s
r
2
w
3
v4
t5
x
6
u
7
yleaves=[y]
Data Structures for Graphs
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
49/69
How to implement breadth-first traversal?
-
8/14/2019 Advanced Data Structures lecture 2
50/69
(2) Expand the leaves of the breadth-first tree in the order in whichthey show up keep the leaves of the tree in a queue(it can be a queuecontainer in C++)
(3) Other desirable features:
Compute the depths of nodes in the breadth-first searchtree (=shortest distance from rootsto that node)Compute the parent of each node in the breadth-firstsearch tree.
r
v t
s
w
x
u y
1
s
r
2
w
3v
4
t5x
6
u
7
Nodes in the order of first visit:
s, r, w, v, t, x, u, yNodes in the order of last visit:
s, r, v, w, t, u, x, y
Data Structures for Graphs
C++implementation of breadth-first traversal
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
51/69
// useful type declarations
typedef string node;
typedef list adjList;
typedef map rootDistance;
typedef map pType;
typedef map graphType;
// auxiliary method -- compute the vector of nodes in the graph
vector *getNodes(graphType graph) {vector *V = new vector();
for(graphType::iterator it = graph.begin();
it != graph.end(); it++) {
V->push_back(it->first);
}
return V;
}// node colors
enum Color {WHITE, BLACK};
Data Structures for Graphs
C++ implementation of breadth-first traversal (contd.)
//
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
52/69
// Breadth-first traversal
// BFS(graph,s,distance,parent) takes inputs graph and s
// and instantiates distance and parent such that:
// - distance[v] returns the distance from v to s
// - parent[v] returns the parent of node v.
void BFS(graphType &graph, node s,
rootDistance &distance, pType &parent) {
vector *V;
V=getNodes(graph);
map nodeColor;
initTables(*V,s,distance,parent,nodeColor);computeBFTables(graph,s,distance,parent,nodeColor);
}
void initTables(vector &V, node s, rootDistance &distance,
pType &parent,map &nodeColor) {
for (unsigned int i=0;i < V.size();i++) {
nodeColor[V[i]]=WHITE;distance[V[i]]=numeric_limits::infinity( );
parent[V[i]]="";
}
nodeColor[s]=BLACK;
distance[s]=0;
}
Data Structures for Graphs
C++ implementation of breadth-first traversal (contd.)
id t BFT bl ( hT h d
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
53/69
void computeBFTables(graphType &graph, node s,
map &distance,pType &parent,map &nodeColor){
queue Q;
Q.push(s);
while(Q.size()!=0) {
node u = Q.front();
Q.pop();
adjList children = graph[u];
// look up the nodes reachable from node u
for(adjList::iterator it=children.begin();
it != children.end(); it++) {
node v = *it;
if (nodeColor[v] == WHITE) {
// mark node v as newly discovered
nodeColor[v] = BLACK;
// record the parent and distance of v from the root
distance[v] = distance[u]+1;
parent[v] = u;// push v in the queue of leaf nodes
Q.push(v);
}
}
}
}
Data Structures for Graphs
Complexity analysis of breadth-first traversal
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
54/69
G= (V, E)
Data Structures for Graphs
Complexity analysis of breadth-first traversal
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
55/69
G= (V, E)
1 Initialization only contains a loop on the nodes of G, therefore ithas linear complexityO(|V|), where|V|is the number of nodesof the graph.
Data Structures for Graphs
Complexity analysis of breadth-first traversal
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
56/69
G= (V, E)
1 Initialization only contains a loop on the nodes of G, therefore ithas linear complexityO(|V|), where|V|is the number of nodesof the graph.
2 Every node of theGis placed only once inQ, and removed onlyonce fromQ. Adding and removing nodes from the queue Qtakes constant time. time for queue operations= O(|V|).
Data Structures for Graphs
Complexity analysis of breadth-first traversal
http://find/http://goback/ -
8/14/2019 Advanced Data Structures lecture 2
57/69
G= (V, E)
1 Initialization only contains a loop on the nodes of G, therefore ithas linear complexityO(|V|), where|V|is the number of nodesof the graph.
2 Every node of theGis placed only once inQ, and removed onlyonce fromQ. Adding and removing nodes from the queue Qtakes constant time. time for queue operations= O(|V|).
3 An enqueued node is processed by scanning at most once thelist of nodes adjacent to it.
NOTEthat the sum of length of all adjacency lists is 2 |E|,therefore the total running time of processing all nodes of GisO(|E|).
Data Structures for Graphs
Complexity analysis of breadth-first traversal
http://find/http://goback/ -
8/14/2019 Advanced Data Structures lecture 2
58/69
G= (V, E)
1 Initialization only contains a loop on the nodes of G, therefore ithas linear complexityO(|V|), where|V|is the number of nodesof the graph.
2 Every node of theGis placed only once inQ, and removed onlyonce fromQ. Adding and removing nodes from the queue Qtakes constant time. time for queue operations= O(|V|).
3 An enqueued node is processed by scanning at most once thelist of nodes adjacent to it.
NOTEthat the sum of length of all adjacency lists is 2 |E|,therefore the total running time of processing all nodes of GisO(|E|).
the total running time ofBFS(G, s)isO(|V| + |E|).
Data Structures for Graphs
Breadth-first traversal: ApplicationPrinting a shortest path between two nodessandt, if it exists
http://find/ -
8/14/2019 Advanced Data Structures lecture 2
59/69
void printPath(graph &G, node s, node v) {
map distance;pType parent;
BFS(G, s, distance, parent);
printPathAux(G,s,v,parent);
cout
-
8/14/2019 Advanced Data Structures lecture 2
60/69
Solves same problem as breadth-first traversal, but in a different way:Given a graphG= (V, E)and a nodesV
Find all nodes reachable by a path from s, by searching"deeper" in the graph whenever possible.
Edges are explored out of the most recentlydiscovered vertexvthat still has unexplorededges leaving it.When all ofvs edges have been explored, thealgorithm "backtracks" to its parent node, toexplore other leaving edges.
Data Structures for Graphs
Depth-first traversal: Example
sr t ur [v], v [r],
http://find/http://goback/ -
8/14/2019 Advanced Data Structures lecture 2
61/69
sr
wv
t
x
u
y
[ ] [ ]s [r, w], w [t, x],t [u, x], x [y],
u [], y [u].
s
r
v
w
t
xu
y
6
31
2 4
5
7
Nodes in the order of first visit: s, r, v, w, t, u, x, y
Nodes in the order of last visit: v, r, u, y, x, t, w, s
Data Structures for Graphs
How to implement depth-first traversal?
http://find/http://goback/ -
8/14/2019 Advanced Data Structures lecture 2
62/69
Data Structures for Graphs
How to implement depth-first traversal?
http://find/http://goback/ -
8/14/2019 Advanced Data Structures lecture 2
63/69
(2) Expand the most recently generated leaf of the depth-first tree; ifit cannot be expanded, remove it, and try alternative expansionsof the parent node keep the leaves of the tree in a queue(it can be a stackcontainer in C++)
Data Structures for Graphs
How to implement depth-first traversal?
http://find/http://goback/ -
8/14/2019 Advanced Data Structures lecture 2
64/69
(2) Expand the most recently generated leaf of the depth-first tree; ifit cannot be expanded, remove it, and try alternative expansionsof the parent node keep the leaves of the tree in a queue(it can be a stackcontainer in C++)
(3) Other desirable features: same as for breadth-first search
Compute the depths of nodes in the depth-first search treeCompute the parent of each node in the depth-first searchtree.
Data Structures for Graphs
AC++implementation of depth-first traversal
http://find/http://goback/ -
8/14/2019 Advanced Data Structures lecture 2
65/69
Same data structures and initialization step like for
breadth-first traversal.// Depth-first search
// DFS(graph,s,distance,parent) takes inputs graph and s
// and instantiates distance and parent such that:
// - distance[v] returns the distance from v to s
// - parent[v] returns the parent of node v.
void DFS(map &graph, node s,rootDistance &distance, pType &parent) {
vector *V;
V=getNodes(graph);
map nodeColor;
initTables(*V,s,distance,parent,nodeColor);
computeDFTables(graph,s,distance,parent,nodeColor);
}
NOTEthe similarities with breadth-first search procedure.
Data Structures for Graphs
AC++ implementation of depth-first traversal (contd.)Iterative implementation
id t DFT bl ( d djLi t h d
http://find/http://goback/ -
8/14/2019 Advanced Data Structures lecture 2
66/69
void computeDFTables(map &graph, node s,
map &distance,pType &parent,map &nodeColor){
stack S;
S.push(s);while(S.size() != 0) {
node u = S.peek();
adjList children = graph[u];
bool found = false;
for(adjList::iterator it=children.begin();
it != children.end(); it++) {
node v = *it;
if (nodeColor[v] == WHITE) {
nodeColor[v] = BLACK;
distance[v] = distance[u]+1;
parent[v] = u;
S.push(v);
found = true;
break;
}
}
if(!found) S.pop();
}
}
Data Structures for Graphs
AC++ implementation of depth-first traversal (contd.)Recursive implementation
http://find/http://goback/ -
8/14/2019 Advanced Data Structures lecture 2
67/69
void computeDFTables(map &graph, node s,
map &distance,pType &parent,map &nodeColor){
nodeColor[s] = BLACK;
adjList children = graph[s];
for(adjList::iterator it=children.begin();
it != children.end(); it++) {
node v = *it;if(nodeColor[v] == WHITE) {
parent[v] = s;
distance[v] = distance[s] + 1;
computeDFTables(graph,v,distance,parent,nodeColor);
}
}
}
Data Structures for Graphs
Remarks about depth-first traversal
http://find/http://goback/ -
8/14/2019 Advanced Data Structures lecture 2
68/69
The implementation differs from breadth-first search byusing a stack container for the nodes that are waiting to be
expanded, instead of a queue.
The behavior isdifferent:
In general, the length of a branch from the root of the tree
to a node in the depth-first tree is different from the lengthof the shortest path from the root to that node.It consumes less memory.
The time complexity of depth-first-search for a graph
G= (V, E)isO(|V| + |E|).
Data Structures for Graphs
References
http://find/http://goback/ -
8/14/2019 Advanced Data Structures lecture 2
69/69
Alfred V. Ahoet al. Data Structures and Algorithms.
Chapter 6: Directed Graphs.
1983. Addison-Wesley.
T. H. Cormen et al. Introduction to Algorithms.Chapter 23: Elementary Graph Algorithms.
2000. MIT Press and McGraw-Hill Book Company.
Data Structures for Graphs
http://find/http://goback/