search related algorithms

34
Search Related Algorithms

Upload: jolie

Post on 23-Feb-2016

29 views

Category:

Documents


0 download

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

Page 1: Search Related Algorithms

Search Related Algorithms

Page 2: 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)

Page 3: Search Related Algorithms

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

Page 4: Search Related Algorithms

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…

Page 5: Search Related Algorithms

BFS Application

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

Page 6: Search Related Algorithms

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

Page 7: Search Related Algorithms

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

Page 8: Search Related Algorithms

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

Page 9: Search Related Algorithms

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

Page 10: Search Related Algorithms

DAG

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

Page 11: Search Related Algorithms

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

Page 12: Search Related Algorithms

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

Page 13: Search Related Algorithms

Detect Cycles

For each vertex:

Vertex Visited In Stack

0 F

1 F

2 F

3 F

4 F

5 F

Page 14: Search Related Algorithms

Detect Cycles

For each vertex:Check 0

Vertex Visited In Stack

0 T T

1 F

2 F

3 F

4 F

5 F

Page 15: Search Related Algorithms

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

Page 16: Search Related Algorithms

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

Page 17: Search Related Algorithms

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

Page 18: Search Related Algorithms

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

Page 19: Search Related Algorithms

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

Page 20: Search Related Algorithms

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

Page 21: Search Related Algorithms

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

Page 22: Search Related Algorithms

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

Page 23: Search Related Algorithms

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

Page 24: Search Related Algorithms

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

Page 25: Search Related Algorithms

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

Page 26: Search Related Algorithms

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

Page 27: Search Related Algorithms

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

Page 28: Search Related Algorithms

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

Page 29: Search Related Algorithms

Topological Sort

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

Page 30: Search Related Algorithms

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

Page 31: Search Related Algorithms

Connected Components

• Connected components :Break graph up intro strongly connected groups

Page 32: Search Related Algorithms

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

Page 33: Search Related Algorithms

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

Page 34: Search Related Algorithms

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)