Search Related Algorithms

Slides:



Advertisements
Similar presentations
CS203 Lecture 15.
Advertisements

1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Charles Lin. Graph Representation Graph Representation DFS DFS BFS BFS Dijkstra Dijkstra A* Search A* Search Bellman-Ford Bellman-Ford Floyd-Warshall.
Search Related Algorithms. Graph Code Adjacency List Representation:
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Representing Graphs Depth First Search Breadth First Search Graph Searching Algorithms.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Breadth-First Search (BFS)
Graphs A New Data Structure
Graphs CSE 2320 – Algorithms and Data Structures Alexandra Stefan
Topological Sorting.
Chapter 28 Graphs and Applications
Chapter 22 Elementary Graph Algorithms
Graphs Representation, BFS, DFS
CSE 373 Topological Sort Graph Traversals
Chapter 3. Decompositions of Graphs
Lecture 11 Graph Algorithms
Fundamentals, Terminology, Traversal, Algorithms
CSE373: Data Structures & Algorithms Lecture 13: Topological Sort / Graph Traversals Kevin Quinn Fall 2015.
CSE 2331/5331 Topic 9: Basic Graph Alg.
Csc 2720 Instructor: Zhuojun Duan
Graph Searching.
Lecture 12 Graph Algorithms
Depth-First Search.
CS120 Graphs.
Data Structures and Algorithms for Information Processing
More Graph Algorithms.
Spanning Trees Longin Jan Latecki Temple University based on slides by
Graph.
Graph Search Applications
Graphs Graph transversals.
Graph & BFS.
Graphs Representation, BFS, DFS
Graphs Chapter 13.
Graphs Chapter 15 explain graph-based algorithms Graph definitions
"Learning how to learn is life's most important skill. " - Tony Buzan
Lectures on Graph Algorithms: searching, testing and sorting
Chapter 11 Graphs.
Graphs Part 2 Adjacency Matrix
Tree Searching.
CSE 373 Data Structures Lecture 16
Richard Anderson Autumn 2016 Lecture 5
COMP171 Depth-First Search.
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Richard Anderson Winter 2009 Lecture 6
Depth-First Search CSE 2011 Winter April 2019.
Algorithms: Design and Analysis
Lecture 6 Graph Traversal
Depth-First Search CSE 2011 Winter April 2019.
A vertex u is reachable from vertex v iff there is a path from v to u.
Chapter 16 1 – Graphs Graph Categories Strong Components
Spanning Trees Longin Jan Latecki Temple University based on slides by
Graphs.
Graphs.
GRAPHS Lecture 17 CS2110 Spring 2018.
Graphs.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Applications of BFS CSE 2011 Winter /17/2019 7:03 AM.
Graphs.
Richard Anderson Lecture 5 Graph Theory
Lecture 10 Graph Algorithms
Lecture 11 Graph Algorithms
Richard Anderson Winter 2019 Lecture 5
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
GRAPH TRAVERSAL.
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 Time : Pick based on job… BFS gets there sooner Time : Adjacency list O(V + E) Build visited list : V Check every edge : V + E Adjacency Matrix O(V2) For each vertex, check every other Pick based on job…

For Search Search Problems BFS : Large memory requirement DFS : Finds suboptimal path May miss answer

IDS Iterative Deepening Search DFS to limited depth Increase depth until solution found Memory bound by search depth Time similar to BFS

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 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 4 5 6 7

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 Cycles For each vertex: Vertex Visited In Stack F 1 2 3 4 5

Detect Cycles For each vertex: Check 0 Vertex Visited In Stack T 1 F 2 T 1 F 2 3 4 5

Detect Cycles For each vertex: Check 0 Check 1 Vertex Visited In Stack T 1 2 F 3 4 5

Detect Cycles For each vertex: Check 0 Check 1 Check 2 Vertex Visited In Stack T 1 2 3 F 4 5

Detect Cycles For each vertex: Check 0 Check 1 Check 2…done Vertex Visited In Stack T 1 2 F 3 4 5

Detect Cycles For each vertex: Check 0 Check 1…done Check 2…done Visited In Stack T 1 F 2 3 4 5

Detect Cycles For each vertex: Check 0 Check 1…done Check 2…done Check2… not needed Vertex Visited In Stack T 1 F 2 3 4 5

Detect Cycles For each vertex: Check 0…done Check 1…done Check 2…done Check2… not needed Vertex Visited In Stack T F 1 2 3 4 5

Detect Cycles For each vertex: Check 1…not needed Vertex Visited In Stack T F 1 2 3 4 5

Detect Cycles For each vertex: Check 2…not needed Vertex Visited In Stack T F 1 2 3 4 5

Detect Cycles For each vertex: Check 3 Vertex Visited In Stack T F 1 2 T F 1 2 3 4 5

Detect Cycles For each vertex: Check 3 Check 2… visited and not on stack Vertex Visited In Stack T F 1 2 3 4 5

Detect Cycles For each vertex: Check 3 Check 2… visited and not on stack Check 4 Vertex Visited In Stack T F 1 2 3 4 5

Detect Cycles For each vertex: Check 3 Check 2… visited and not on stack Check 4 Check 5 Vertex Visited In Stack T F 1 2 3 4 5

Detect Cycles For each vertex: Check 3 Check 2… visited and not on stack Check 4 Check 5 Check 3… already on stack! Vertex Visited In Stack T F 1 2 3 4 5

Detect Cycles bool checkCycle() Make bool isVisited[] – every vertex to false Make bool inStack[] – every vertex to false For each vertex If not already visited if cycleDFS(vertex, isVisited, inStack) return true return 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 ordering Make bool isVisited[] – every vertex to false For 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 = 0 Make bool isVisited[] – every vertex to false For 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 lastVisitTime Reverse all edges Make bool isVisited[] – every vertex to false For 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)