Graph Traversals A Search Problem A Non

4/29/14
Graph Traversals A Search Problem •  A graph-­‐traversal algorithm •  High Planes Airline Company (HPAir) Problem –  Visits all the ver6ces that it can reach star6ng at some vertex –  Visits all ver6ces of the graph iff the graph is connected (effec6vely compu6ng Connected Components) –  Must not loop forever, if a graph contains a cycle –  Must never visit a vertex more than once •  connected component (for undirected graphs) = –  The subset of ver6ces visited during a traversal that begins at a given vertex •  strongly connected component (for directed graphs) = –  For each customer request, indicate whether a sequence of HPAir flights exists from the origin city to the des6na6on city •  The flight map for HPAir is a graph –  Arc between ver6ces •  There is a flight between ci6es –  Directed path •  There is a sequence of flight connec6ons –  The subset of ver6ces visited during a traversal that begins at any of its members A Non-­‐Recursive Solu6on That Uses a Stack DFS(originCity): Searching the Flight Map •  The solu6on performs an exhaus6ve search stk = new Stack<E>();!
–  Beginning at the origin city, tries every possible sequence of flights un6l either stk.push(originCity);!
!
while (a sequence of flights from originCity to destinCity
•  Finds a sequence that gets to the des6na6on city •  Determines that no such sequence exists has not been found) {!
!if (you cannot go anywhere from the city on top of stack)!
!
!stk.pop(); // backtrack!
!else select a neighbor, anotherCity,
•  The ADT Stack is useful in organizing an exhaus6ve search –  It helps you remember how you got the the current point from the city on top of stack;!
!stk.push(anotherCity);!
!
}!
•  Backtracking can be used to recover from a wrong choice of a city P
R
P
X
R
P
R
P
P
W
P
S
W
P
T
S
W
P
W
T
S
W
P
1
4/29/14
... and remember where you’ve been Depth-­‐First-­‐Search Example: From P-­‐>Z stk = new Stack<E>(); Clear Marks;!
stk.push(originCity);!
Mark(originCity) as visited;!
while (a sequence of flights from originCity to destinCity has not been found) !
List visited (marked)
{!
if (you cannot find an unvisited city from the city on top of stack)!
stk.pop(); // backtrack!
P
R
X
W S
T
Y
Z
!else select an unvisited neighbor, anotherCity,
from the city on top of stack;!
stk.push(anotherCity);!
Stack stk
Mark(anotherCity) as visited;!
}!
P
Would DFS(oC) work for undirected graphs? Z
stk = new Stack<E>(); Clear Marks;!
stk.push(originCity);!
Mark(originCity) as visited;!
while (a sequence of flights from originCity to destinCity Y
has not been found) !
{!
if (you cannot find an unvisited city from the city on top of stack)!
stk.pop();
W
!else select an unvisited neighbor, anotherCity,
from the city on top of stack;!
R
P
X
Q
stk.push(anotherCity);!
Mark(anotherCity) as visited;!
}!
P
R
P
X
R
P
S
// backtrack!
T
R
P
X
R
P
R
P
P
W
P
S
W
P
T
S
W
P
S
W
P
W
P
Y
W
P
Z
Y
W
P
Labyrinth: Umberto Eco advises Theseus “To find the way out of a labyrinth there is
only one means. At every new junction,
never seen before, the path we have taken
will be marked with three signs. If … you
see that the junction has already been
visited, you will make only one mark on the
path you have taken. If all the apertures
have already been marked, then you must
retrace your steps. But if one or two
apertures of the junction are still without
signs, you will choose any one, making two
signs on it. Proceeding through an aperture
that bears only one sign, you will make two
more, so that now the aperture bears three.
All the parts of the labyrinth must have been
visited if, arriving at a junction, you never
take a passage with three signs, unless none
of the other passages is now without signs.”
2
4/29/14
Mazes as Graphs A
C
B
E
D
B
A
C
B
A
START
EXIT
F
G
A
B
A
H
D
B
A
E
D
B
A
D
B
A
C
B
A
H
E
D
F
G
B
A
A
F
A
G
F
A
H
G
F
A
Tes6ng for Connec6vity using DFS(oC) Connected: An undirected graph for which there is a path from any node to any other node Is this graph connected? Connected component: A connected sub-­‐graph Can we use DFS to find all connected components? Strong Connec6vity Z
Y
W
R
P
X
Q
S
T
Strongly Connected: A graph for which there is a directed path from any node to any other node Is this graph strongly connected? Strongly connected component: A strongly connected sub-­‐graph Can you find the strongly connected components of this graph? 3
4/29/14
The Web Graph -­‐ Star6ng at CS230 Home The Web is a Graph URL
http://cs.wellesley.edu/~cs230/PPTs/Hash.html
CS230
Access method
Server and domain
Path
Document
" Directed Graph of Nodes and Arcs n 
n 
Nodes = web pages Arcs = hyperlinks from a page to another Syllabus
Assignments
Instructor
...
...
...
CS Dept.
Textbook
Documentation
PLTC
" A graph can be explored " A graph can be indexed Searching the Web •  The web can be considered a graph “the web graph” •  Web pages are the graph nodes •  Hyperlinks on pages are graph edges •  The web graph is huge (way over one million billions nodes) – maybe infinite (pages are created on the fly) •  For the web graph, DFS is not a good strategy. (Why?) •  You need to search your neighborhood before going deeper ...
...
...
Breadth First Example: BFS(9) 1
The shape of the Web is … a “bow-­‐Ee”(!) 2
4
3
5
7
6
8
9
Queue: 9
6
7
8
3
4
5
1
2
Iterator: 1-16
4
4/29/14
How do you remember the path? BFS(v) pseudocode a
// BFS traversal star6ng at v Ini%aliza%on: Mark all ver6ces as unvisited enQueue v onto a new queue Q Mark v as visited While (Q is not empty) a
b
i
S
c
e
S
G
h
f
deQueue a vertex w from Q For each unvisited vertex u adjacent to w: S
enQueue u onto Q Mark u as visited BFS from S to G: The BFS tree shows the visits How do you remember the path? i h e b c a [Sa]
Sa
Sh
Sh
Sai
Sab
Sai
Sab
She
Sab
She
ShG
c
h [Sh]
b [Sab]
e [She]
G [ShG]
f
ShG
c [Sabc]
f [ShGf]
ShG
Sabc
ShG
Sabc
G f
Resolving DAG Dependencies •  Defined on a Directed Acyclic Graph (a “DAG”) •  Usually reflect dependencies or requirements •  I.e., Assembly lines, Supply lines, Organiza6onal charts, … •  BTW: You cannot take 231 aier 230 unless… •  Understanding dependencies requires “topological sor6ng” •  Topological order 232
231
225
110
230
240
215
111
242
G
h
Dependency Graph on a DAG 114
e
S
i [Sai]
b
IniEalizaEon: enqueue path [S] in Q While you have not reached G dequeue a path from BFS queue and … in the next step it will reach the goal G
check the last node x in the path extend the path to unvisited neighbors of x and enqueue extended paths to back of Q She
a
i
S [S]
235
–  A list of ver6ces in a DAG such that vertex x precedes vertex y iff there is a directed edge from x to y in the graph –  There may be several topological orders in a given graph •  Topological sor6ng –  Arranging the ver6ces into a topological order a
251
315
b
d
c
e
f
g
5
4/29/14
A(nother) Topological Sor6ng Algorithm A Topological Sor6ng Algorithm •  Select a vertex v that has no predecessor •  Select a vertex v that has no successor a
•  Remove v from the graph (along with all associated arcs), b
c
d
•  Add v to the end of a list of ver6ces L e
a
•  Remove v from the graph (along with all associated arcs), f
•  Repeat previous steps •  When the graph is empty, L’s ver6ces will be in topological order •  When the graph is empty, L’s ver6ces will be in topological order g
d
b
e
c
f
e
f
g
•  Repeat previous steps a
c
d
•  Add v to the beginning of a list of ver6ces L g
b
a
g
d
b
e
c
f
6