search related algorithms

Post on 23-Feb-2016

29 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Search Related Algorithms. DFS vs BFS. BFS - Spreads out in all directions. DFS vs BFS. BFS - Spreads out in all directions 1 Hop. DFS vs BFS. BFS - Spreads out in all directions 1 Hop 2 Hops. DFS vs BFS. BFS - Spreads out in all directions 1 Hop 2 Hops 3 Hops. - PowerPoint PPT Presentation

TRANSCRIPT

Search Related Algorithms

BFS

• Finds shortest path to all connected vertices• Space:– Exponential in search depth • Degree of 100 for each vertex

– First hop – 100 vertices on queue– Second hop - ~10,000 vertices on queue– Third hop - ~1,000,000 vertices to track

– But limited by number of vertices : O(V)

DFS

• Finds all connected vertices• Space– Proportional to length of current path– Recursive version implicitly stores on call stack– Iterative version explicitly stores on stack• May have duplicates

DFS vs BFS

• Both worst case O(V) space– BFS gets there sooner

• Time : – Adjacency list O(V + E)

• Build visited list : V• Check every edge : E

– Adjacency Matrix O(V2)• For each vertex, check every other

• Pick based on pattern– BFS for shortest path, bipartite coloring, etc…– DFS for connectivity…

BFS Application

• Bipartite : can divide graph into two sets of vertices A, B; every edge connects an element of A to element of B

BFS Application

• Storage array for color• Mark start vertex blue• When visiting a node, color neighbors opposite color– Conflict = not bipartite

Vertex Color

1

2

3

4

5

6

7

BFS Application

• Storage array for color• Mark start vertex blue• When visiting a node, color neighbors opposite color– Conflict = not bipartite

Vertex Color

1

2 Blue

3

4

5

6

7

BFS Application

• Storage array for color• Mark start vertex blue• When visiting a node, color neighbors opposite color– Conflict = not bipartite

Vertex Color

1 Red

2 Blue

3 Red

4

5

6 Red

7

BFS Application

• Storage array for color• Mark start vertex blue• When visiting a node, color neighbors opposite color– Conflict = not bipartite

Vertex Color

1 Red

2 Blue

3 Red

4 Blue

5 Blue

6 Red

7 Blue

DAG

• DAGs : Directed Acyclical Graphs– Common in scheduling type problems

Cycle Detection

• Detect Cycle:– Do DFS from each node until

all are visited• If a neighbor to current is

already on stack we have a cycle

Detect Cyclesbool checkCycle()Make bool isVisited[] – every vertex to falseMake bool inStack[] – every vertex to falseFor each vertex If not already visited if cycleDFS(vertex, isVisited, inStack) return truereturn false

bool cycleDFS(vertex, isVisited, inStack) mark vertex inStack mark vertex visited for each neighbor if in stack return true //cycle!! if not visited if cycleDFS(neighbor, isVisited, inStack) return true //found a cycle down stream mark vertex not inStack return false //guess we were OK along this path

Detect Cycles

For each vertex:

Vertex Visited In Stack

0 F

1 F

2 F

3 F

4 F

5 F

Detect Cycles

For each vertex:Check 0

Vertex Visited In Stack

0 T T

1 F

2 F

3 F

4 F

5 F

Detect Cycles

For each vertex:Check 0 Check 1

Vertex Visited In Stack

0 T T

1 T T

2 F

3 F

4 F

5 F

Detect Cycles

For each vertex:Check 0 Check 1 Check 2 Vertex Visited In Stack

0 T T

1 T T

2 T T

3 F

4 F

5 F

Detect Cycles

For each vertex:Check 0 Check 1 Check 2…done Vertex Visited In Stack

0 T T

1 T T

2 T F

3 F

4 F

5 F

Detect Cycles

For each vertex:Check 0 Check 1…done Check 2…done Vertex Visited In Stack

0 T T

1 T F

2 T F

3 F

4 F

5 F

Detect Cycles

For each vertex:Check 0 Check 1…done Check 2…done Check2… not needed

Vertex Visited In Stack

0 T T

1 T F

2 T F

3 F

4 F

5 F

Detect Cycles

For each vertex:Check 0…done Check 1…done Check 2…done Check2… not needed

Vertex Visited In Stack

0 T F

1 T F

2 T F

3 F

4 F

5 F

Detect Cycles

For each vertex:Check 1…not needed

Vertex Visited In Stack

0 T F

1 T F

2 T F

3 F

4 F

5 F

Detect Cycles

For each vertex:Check 2…not needed

Vertex Visited In Stack

0 T F

1 T F

2 T F

3 F

4 F

5 F

Detect Cycles

For each vertex:Check 3

Vertex Visited In Stack

0 T F

1 T F

2 T F

3 T T

4 F

5 F

Detect Cycles

For each vertex:Check 3 Check 2…not needed

Vertex Visited In Stack

0 T F

1 T F

2 T F

3 T T

4 F

5 F

Detect Cycles

For each vertex:Check 3 Check 2…not needed Check 4 Vertex Visited In Stack

0 T F

1 T F

2 T F

3 T T

4 T T

5 F

Detect Cycles

For each vertex:Check 3 Check 2…not needed Check 4 Check 5

Vertex Visited In Stack

0 T F

1 T F

2 T F

3 T T

4 T T

5 T T

Detect Cycles

For each vertex:Check 3 Check 2…not needed Check 4 Check 5 Check 3… already on stack!

Vertex Visited In Stack

0 T F

1 T F

2 T F

3 T T

4 T T

5 T T

Detect Cyclesbool checkCycle()Make bool isVisited[] – every vertex to falseMake bool inStack[] – every vertex to falseFor each vertex If not already visited if cycleDFS(vertex, isVisited, inStack) return truereturn false

bool cycleDFS(vertex, isVisited, inStack) mark vertex inStack mark vertex visited for each neighbor if in stack return true //cycle!! if not visited if cycleDFS(neighbor, isVisited, inStack) return true //found a cycle down stream mark vertex not inStack return false //guess we were OK along this path

Topological Sort

• Topological Sort : turn DAG into ordered list such that all edges point one way– Maybe multiple orderings

Topological Sort

• Do DFS• Add node to list as you LEAVE Make list orderingMake bool isVisited[] – every vertex to falseFor each vertex

If not visited, do topoDFS(vertex, isVisited, ordering)

topoDFS(vertex, isVisited, ordering) mark vertex visited for each neighbor if not visited topoDFS(neighbor, isVisited, ordering) add vertex to front of ordering

Connected Components

• Connected components :Break graph up intro strongly connected groups

Connected Components

• Connected components :– Phase 1 : Identify chains / "roots"• Do DFSs until each vertex visited• Record time each vertex left the stack (last visited)

– Phase 2 : Find connections that lead to "roots"• Sort list of vertexes based on last visit time (large to small)• Reverse all the edges• Do DFS from each node based on sorted list until every

node has a group

Connected Components

• Connected components – part 1 Make int lastVisitTime[]int time = 0Make bool isVisited[] – every vertex to falseFor each vertex

If not visited, do connectDFS(vertex, isVisited, lastVisit, time)

connectDFS(vertex, isVisited, lastVisit, &time) mark vertex visited time++ for each neighbor if not visited connectDFS(neighbor, isVisited, lastVisit, time) lastVisit[vertex] = ++time

Connected Components

• Connected components – part 2 Make masterlist of vertices – sorted based on lastVisitTimeReverse all edgesMake bool isVisited[] – every vertex to falseFor each vertex in list If not visited, Make curConnected list do connect2DFS(vertex, isVisited, curConnected) print curConnected

connect2DFS(vertex, isVisited, curConnected) mark vertex visited add to curConnected for each neighbor if not visited connect2DFS(neighbor, isVisited, curConnected)

top related