Graph Searching.

Slides:



Advertisements
Similar presentations
Graph Theory, DFS & BFS Kelly Choi What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic.
Advertisements

Graph and String Matching String Matching Problem Given a text string T of length n and a pattern string P of length m, the exact string matching.
Advanced Graph Modelling and Searching HKOI Training 2010.
Advanced DFS & BFS HKOI Training Advanced D epth - F irst S earch and B readth- F irst S earch.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Chapter 8, Part I Graph Algorithms.
Graph & BFS.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
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.
CS261 Data Structures DFS and BFS – Edge List Representation.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Graph Representation, DFS and BFS Gary Wong If you have any question, please ask via MSN:
Graph Theory Kayman Lui Overview Graph –Notation and Implementation –Tree Depth First Search (DFS) –DFS Forests Topology Sort (T-Sort) Strongly.
Advanced Graph Modelling and Searching HKOI Training 2008.
1 Advanced DFS, BFS, Graph Modeling 25/2/ Introduction Depth-first search (DFS) Breadth-first search (BFS) Graph Modeling –Model a graph from a.
Advanced DFS, BFS, Graph Modeling
Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
Introduction to Graph Theory HKOI Training (Intermediate) Kelly Choi 29 Mar 2008.
Introduction to Algorithms
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Breadth-First Search (BFS)
Graphs A New Data Structure
Graphs Breadth First Search & Depth First Search
Graph Representation, DFS and BFS
Graphs Chapter 20.
Graphs Representation, BFS, DFS
CSC317 Graph algorithms Why bother?
CSE 2331/5331 Topic 9: Basic Graph Alg.
Csc 2720 Instructor: Zhuojun Duan
Depth-First Search.
Graphs Breadth First Search & Depth First Search
Unweighted Shortest Path Neil Tang 3/11/2010
CS120 Graphs.
Graph Algorithms Using Depth First Search
CSCE 411 Design and Analysis of Algorithms
Graph.
Graph Search Applications
Lecture 10 Algorithm Analysis
Graph & BFS.
Graphs Representation, BFS, DFS
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Elementary graph algorithms Chapter 22
Search Related Algorithms
Chapter 22: Elementary Graph Algorithms I
Graph Representation (23.1/22.1)
Graphs.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
CSE 373 Data Structures Lecture 16
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Depth-First Search CSE 2011 Winter April 2019.
Algorithms: Design and Analysis
Graphs Chapter 7 Visit for more Learning Resources.
Chapter 16 1 – Graphs Graph Categories Strong Components
Elementary graph algorithms Chapter 22
Important Problem Types and Fundamental Data Structures
3.2 Graph Traversal.
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 .
Presentation transcript:

Graph Searching

Overview Graph BFS (Breadth First Search) DFS (Depth First Search) Notation and Implementation BFS (Breadth First Search) DFS (Depth First Search) Topology SCC (Strongly Connected Component) Centre, Radius, Diameter Multi-state BFS

What is Graph?

Graph A graph is defined as G=(V,E), i.e a set of vertices and edges, where V is the set of vertices (singular: vertex) E is the set of edges that connect some of the vertices WA NT SA Q NSW V T 1 2 4 3 vertex edge

Graph Directed/Undirected Graph Weighted/Unweighted Graph Connectivity

Representation of Graph Adjacency Matrix Adjacency list Edge list

Representation of Graph Adjacency Matrix Adjacency Linked List Edge List Memory Storage O(V2) O(V+E) Check whether (u,v) is an edge O(1) O(deg(u)) Find all adjacent vertices of a vertex u O(V) deg(u): the number of edges connecting vertex u

Graph Tree root siblings descendents children ancestors parent

Depth-First Search (DFS) Strategy: Go as far as you can (if you have not visit there), otherwise, go back and try another way

Implementation Initially all vertices are marked as unvisited DFS (vertex u) { mark u as visited for each vertex v directly reachable from u if v is unvisited DFS (v) } Initially all vertices are marked as unvisited

Topological Sort Topological order: A numbering of the vertices of a directed acyclic graph such that every edge from a vertex numbered i to a vertex numbered j satisfies i<j Topological Sort: Finding the topological order of a directed acyclic graph

Tsort Algorithm If the graph has more then one vertex that has indegree 0, add a vertice to connect to all indegree-0 vertices Let the indegree 0 vertice be s Use s as start vertice, and compute the DFS forest The death time of the vertices represent the reverse of topological order

Example: Assembly Line In a factory, there is several process. Some need to be done before others. Can you order those processes so that they can be done smoothly? Chris is now studying OI materials. There are many topics. He needs to master some basic topics before understanding those advanced one. Can you help him to plan a smooth study plan?

Example: SCC A graph is strongly-connected if for any pair of vertices u and v, one can go from u to v and from v to u. Informally speaking, an SCC of a graph is a subset of vertices that forms a strongly-connected subgraph does not form a strongly-connected subgraph with the addition of any new vertex

SCC (Illustration)

Finding Shortest Path How can we use DFS to find the shortest distance from a vertex to another? The distance of the first path found?

Breadth-First Search (BFS) BFS tries to find the target from nearest vertices first. BFS makes use of a queue to store visited (but not dead) vertices, expanding the path from the earliest visited vertices.

Simulation of BFS Queue: 1 4 3 5 2 6 1 4 3 2 5 6

Implementation while queue Q not empty dequeue the first vertex u from Q for each vertex v directly reachable from u if v is unvisited enqueue v to Q mark v as visited Initially all vertices except the start vertex are marked as unvisited and the queue contains the start vertex only

Advantages Guarantee shortest paths for unweighted graphs Use queue instead of recursive functions – Avoiding stack overflow

Flood Fill An algorithm that determines the area connected to a given node in a multi-dimensional array Start BFS/DFS from the given node, counting the total number of nodes visited Example: Squareland (HKOI 2006)

Variations of BFS and DFS Bidirectional Search (BDS) Iterative Deepening Search(IDS)

Bidirectional search (BDS) Searches simultaneously from both the start vertex and goal vertex Commonly implemented as bidirectional BFS start goal

BDS Example: Bomber Man (1 Bomb) find the shortest path from the upper-left corner to the lower-right corner in a maze using a bomb. The bomb can destroy a wall. S E

Bomber Man (1 Bomb) S 1 2 3 4 E 13 12 11 12 4 10 8 7 6 5 6 7 8 9 9 5 10 11 12 13 4 3 2 1 Shortest Path length = 8 What will happen if we stop once we find a path?

Example S 1 2 3 4 5 6 7 8 9 10 21 11 20 12 19 13 18 14 17 16 15 E

Iterative deepening search (IDS) Iteratively performs DFS with increasing depth bound Shortest paths are guaranteed

IDS

IDS (pseudo code) DFS (vertex u, depth d) { mark u as visited if (d>0) for each vertex v directly reachable from u if v is unvisited DFS (v,d-1) } i=0 Do { DFS(start vertex,i) Increment i }While (target is not found)

IDS The complexity of IDS is the same as DFS

Summary of DFS, BFS We learned some variations of DFS and BFS Bidirectional search (BDS) Iterative deepening search (IDS)

Equation Question: Find the number of solution of xi, given ki,pi. 1<=n<=6, 1<=xi<=150 Vertex: possible values of k1x1p1 , k1x1p1 + k2x2p2 , k1x1p1 + k2x2p2 + k3x3p3 , k4x4p4 , k4x4p4 + k5x5p5 , k4x4p4 + k5x5p5 + k6x6p6