Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graph Theory, DFS & BFS Kelly Choi 08-07-2006. 08-07-20062 What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic.

Similar presentations


Presentation on theme: "Graph Theory, DFS & BFS Kelly Choi 08-07-2006. 08-07-20062 What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic."— Presentation transcript:

1 Graph Theory, DFS & BFS Kelly Choi 08-07-2006

2 08-07-20062 What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic vertex edge

3 08-07-20063 Representation of Graphs Adjacency Matrix –A V x V array, with matrix[i][j] storing whether there is an edge between the i th vertex and the j th vertex Adjacency Linked List –One linked list per vertex, each storing directly reachable vertices Edge List

4 08-07-20064 Representation of Graphs Adjacency Matrix Adjacency Linked List Edge List Memory Storage O(V 2 )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)O(deg(u)) deg(u): the number of edges connecting vertex u

5 08-07-20065 Graph Searching Why do we do graph searching? What do we search for? What information can we find from graph searching? How do we search the graph? Do we need to visit all vertices? In what order?

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

7 08-07-20067 Implementation 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

8 08-07-20068 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

9 08-07-20069 Example: Teacher’s Problem 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

10 08-07-200610 Shortest Paths? Given vertices s and t, if we try to find a path from s to t by DFS, the first path found may not be the shortest one. How can I find a shortest path? Let’s first review what we have done

11 08-07-200611 An Idea of What We Did 1.Start by visiting a certain vertex 2.Vertices fall into 3 categories: –Unvisited –Visited –Visited & Dead (All reachable vertices from these vertices are visited)

12 08-07-200612 An Idea of What We Did 3.Find unvisited vertices by expanding your path from visited (but not dead) vertices. In DFS, we choose the most recently visited vertex to expand. Are there any other strategies?

13 08-07-200613 Breadth-First Search (BFS) Instead of going as far as possible, BFS tries to search all paths. BFS makes use of a queue to store visited (but not dead) vertices, expanding the path from the earliest visited vertices.

14 08-07-200614 1 4 3 2 5 6 Simulation of BFS Queue: 143 5 26

15 08-07-200615 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

16 08-07-200616 There is more… Other Graph Searching Algorithms: –Bidirectional search (BDS) –Iterative deepening search (IDS)

17 08-07-200617 Advantages Guarantee shortest paths for unweighted graphs Use queue instead of recursive functions – Avoiding stack overflow

18 08-07-200618 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)

19 08-07-200619 Graph Modeling Conversion of a problem into a graph problem Essential in solving most graph problems

20 08-07-200620 Basics of graph modeling Identify the vertices and the edges Identify the objective of the problem State the objective in graph terms Implementation: –construct the graph from the input instance –run the suitable graph algorithms on the graph –convert the output to the required format (cx, 2004)

21 08-07-200621 Other Topics in Graph Theory Cut Vertices & Cut Edges Euler Path/Circuit & Hamilton Path/Circuit Planarity

22 08-07-200622 Cut Vertices & Cut Edges What is a cut vertex? –A cut vertex is a type of graph vertex, the removal of which causes an increase in the number of connected components What is a cut edge?

23 08-07-200623 Euler Path & Hamilton Path An Euler path is a path in a graph which visits each edge exactly once A Hamilton path is a path in an undirected graph which visits each vertex exactly once.

24 08-07-200624 Planarity A planar graph is a graph that can be drawn so that no edges intersect K 5 and K 3,3 are non-planar graphs


Download ppt "Graph Theory, DFS & BFS Kelly Choi 08-07-2006. 08-07-20062 What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic."

Similar presentations


Ads by Google