Graph Representation, DFS and BFS

Slides:



Advertisements
Similar presentations
Depth-First Search1 Part-H2 Depth-First Search DB A C E.
Advertisements

Graph Theory, DFS & BFS Kelly Choi What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic.
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.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Data Structure and Algorithms (BCS 1223) GRAPH. Introduction of Graph A graph G consists of two things: 1.A set V of elements called nodes(or points or.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Using Search in Problem Solving
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.
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
COSC 2007 Data Structures II Chapter 14 Graphs III.
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.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
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.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Introduction to Graph Theory HKOI Training (Intermediate) Kelly Choi 29 Mar 2008.
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Graphs A New Data Structure
Graphs and Graph Algorithms
Graphs Chapter 20.
A vertex u is reachable from vertex v iff there is a path from v to u.
Graphs Representation, BFS, DFS
CSE 373 Topological Sort Graph Traversals
Fundamentals, Terminology, Traversal, Algorithms
CSC317 Graph algorithms Why bother?
Csc 2720 Instructor: Zhuojun Duan
Graph Searching.
Depth-First Search.
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
Spanning Trees Longin Jan Latecki Temple University based on slides by
Graph Representation, DFS and BFS
Graph & BFS.
Graphs Representation, BFS, DFS
Graphs Chapter 13.
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Graphs.
Graphs.
CSE 373: Data Structures and Algorithms
Depth-First Search D B A C E Depth-First Search Depth-First Search
Chapter 11 Graphs.
Connected Components, Directed Graphs, Topological Sort
Yan Shi CS/SE 2630 Lecture Notes
Tree Searching.
Graph Theory By Amy C. and John M..
Subgraphs, Connected Components, Spanning Trees
Algorithms: Design and Analysis
Graphs Chapter 7 Visit for more Learning Resources.
A vertex u is reachable from vertex v iff there is a path from v to u.
Graphs.
Spanning Trees Longin Jan Latecki Temple University based on slides by
Graphs.
Graphs.
Graphs.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Graphs.
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 Representation, DFS and BFS If you have any question, please ask via Email: garywong612@gmail.com MSN: gary_wong612@hotmail.com Graph Representation, DFS and BFS Gary Wong

WARNING! Today’s topics are: Tough Essential Probably the hardest among all intermediate topics Essential Foundation of many advanced algorithms/concepts, such as the topics in the afternoon session

But don’t panic… I will guide you throughout the training Don’t be shy to interrupt and ask ^^

Pre-requisites Before we start, I would like to ensure that you know the followings: Queue Linked List Recursion

A classical problem Now I have N friends, but I have the phone numbers of some of them only I want to deliver a message to Sarah by repeated calling My memory is so good that I remember who can be contacted by my N friends Give me a calling plan for me and my friends

A classical problem This is actually a graph! Winnie Ryan Gary Ada Joyce Ryan John Ada Sarah Winnie

What is a graph? A set of vertices (or nodes) linked by edges Mathematically, we often write G = (V,E) V: set of vertices, so |V| = number of vertices E: set of edges, so |E| = number of edges 1 3 2 5 4

Why do we need graphs? To present the relationships between different objects/elements in a mathematical way Examples: Friendship Local Area Network (LAN) Map of a country What could the vertices and edges represent in the above examples?

Various types of graphs Connected/disconnected graphs The circled subgraphs are also known as connected components

Various types of graphs Directed/undirected graphs You may treat each undirected edge as two directed edges in opposite directions

Various types of graphs Weighted/unweighted graphs You may treat unweighted edges to be weighted edges of equal weights 5 -2 7 4

Special graphs Planar graphs A graph that can be drawn on a plane without edge intersections The following two graphs are equivalent and planar: To be discussed in details in Graph (III) 

Special graphs Tree NO!

Special graphs Tree: either one of the followings is the definition A connected graph with |V|-1 edges A connected graph without cycles A graph with exactly one path between every pair of vertices

Special graphs Tree edges could be directed or undirected For trees with directed edges, a root usually exists

Special graphs Forest All connected component(s) is/are tree(s) How many trees are there in the following forest?

How to store graphs in the program? Usually, the vertices are labeled beforehand 3 types of graph representations: Adjacency matrix Adjacency list Edge list 1 3 4 2 5 -2 7

Adjacency matrix Use a 2D array 1 3 4 2 5 -2 7 s\t 1 2 3 4 5 -2 7

Adjacency matrix Memory complexity? Time complexity for: Checking the weight of an edge between 2 given nodes? Querying all adjacent nodes of a given node?

Adjacency list N vertices, N linked lists Each list stores its adjacent vertices 1 3 4 2 5 -2 7 1 5|-2 3|5 2 1|0 3|7 3 4 2|4 5

Adjacency list Memory complexity? Time complexity for: Checking the weight of an edge between 2 given nodes? Querying all adjacent nodes of a given node?

Edge list A list of edges 1 3 4 2 5 -2 7 id x y w 1 5 -2 2 3 7 4

Edge list Memory complexity? Time complexity for: Checking the weight of an edge between 2 given nodes? Querying all adjacent nodes of a given node?

Which one should be used? It depends on: Constraints Time Limit Memory Limit What algorithm is used

Back to the classical problem… How can we solve this? Equivalent to finding a path from “Gary” to “Sarah” Gary Joyce Ryan John Ada Sarah Emily

Back to the classical problem… For convenience, we label the nodes by numbers instead of names Gary Joyce Ryan John Ada Sarah Emily 7 2 1 3 5 6 4

Let’s use recursion! Go as far as you can If it is a blind end, go back and walk through another edge 7 2 1 3 5 6 4

Let’s use recursion! This is also known as Depth-First-Search (DFS) DFS (node u) { mark u as visited for each adjacent node v from u if (v is unvisited) DFS (v) } Initialize all nodes as unvisited

Depth First Search (DFS) Let’s review the graph, and note the color changes Green: Unvisited Grey: Visited Black: Dead (No more unvisited adjacent nodes) 7 2 1 3 5 6 4

Depth First Search (DFS) Advantages Useful for checking whether 2 nodes are connected Drawbacks Finding a shortest path using DFS is difficult Stack overflow

A similar classical problem Same situation, but I want the calling plan with the least number of calls Equivalent to finding the shortest path from “Gary” to “Sarah”

Breadth First Search (BFS) Go to all nearest nodes first The data structure “queue” is used to store the visited nodes Expand from visited (but not dead) nodes

Breadth First Search (BFS) Queue 1 6 2 5 7 3 4 The path has been found! But let’s complete the BFS… 7 2 1 3 5 6 4

Breadth First Search (BFS) while queue Q not empty dequeue the first node u from Q for each adjacent node v from u if v is unvisited enqueue v to Q mark v as visited Initialize all nodes as unvisited, except the starting node

Breadth First Search (BFS) Advantages Shortest route is guaranteed on unweighted graphs How about weighted graphs? Avoid stack overflow Why don’t we always use BFS instead of DFS?

Question Yes, you can show that there exists a path to the destination, but how to trace the path?

Let’s have a 10-minute break! We will discuss some basic applications of graphs

Maze Given an N x N maze, find the shortest route from S to E. BFS S E 1,2 1,3 1,4 2,1 2,2 2,3 2,4 3,1 3,2 3,3 E 4,1 4,2 4,3 4,4

Maze In fact you don’t even need to store the graph in this way The changes are the current coordinates only Just directly mark on the map! Use the coordinates to represent the states (狀態) when you do BFS

Variants of maze problems Different scenarios: You may have a few bombs to destroy walls… Someone is chasing after you, you need to escape without being caught… Walls can be pushed forward… No matter how complicated it is, the main concern is how you should represent a state

Finding area Find the area that are reachable from A. A

Flood fill Starting from 1 vertex, use DFS / BFS to repeatedly visit (or fill) the adjacent vertices Count the number of visited vertices while you do DFS / BFS

Teacher’s Problem HKOI 2004 Senior Emily wants to distribute candies to N students one by one, with a rule that if student A is teased by B, A can receive candy before B. Given lists of students teased by each students, find a possible sequence to give the candies

Teacher’s Problem In short, in a directed graph, We want to give a label to the vertices So that if there is an edge from u to v, then u < v Is it always possible? Finding such order is called “topological sort”

Topological sort Find a vertex with no incoming edges. Number it and remove all outgoing edges from it. Repeat the process. How can it be implemented by: DFS? BFS?

More (if time allows) Iterative Deepening Search (IDS) Bidirectional Search (BDS)

Exercises 1035 Patrol Area 1067 Maze 1075 Sokoban 1114 Theseus and the Minotaur 2037 Largest Continuous Region 2045 Teacher’s Problem 3012 OIMan 3021 Bomber Man 3040 Amazing Robot II