Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Graph Theory HKOI Training (Intermediate) Kelly Choi 29 Mar 2008.

Similar presentations


Presentation on theme: "Introduction to Graph Theory HKOI Training (Intermediate) Kelly Choi 29 Mar 2008."— Presentation transcript:

1 Introduction to Graph Theory HKOI Training (Intermediate) Kelly Choi 29 Mar 2008

2 Overview Ideas of Graphs Ideas of Graphs Visiting a graph Visiting a graph BFS (Breadth First Search) BFS (Breadth First Search) DFS (Depth First Search) DFS (Depth First Search) Topological Sort Topological Sort Flood Fill Flood Fill Graph Representation Graph Representation Other Topics Other Topics

3 A Classical Problem Maze Maze Find a path from S to E Find a path from S to E S E

4 Passing News In a group of people, you need to pass a piece of news to someone you don’t know. In a group of people, you need to pass a piece of news to someone you don’t know. You know who knows who and you need to contact someone you know so that the message can be passed to the N th person. You know who knows who and you need to contact someone you know so that the message can be passed to the N th person.

5 Did the two problems look similar? Essentially we have some “units” which are linked together, and we want to find a way to get from one unit to another. Essentially we have some “units” which are linked together, and we want to find a way to get from one unit to another. There are many other problems with some “units” and some “links”. We model them with the “vertices” and “edges” of graphs. There are many other problems with some “units” and some “links”. We model them with the “vertices” and “edges” of graphs.

6 WHAT ARE GRAPHS?

7 Graph In a graph, we have some vertices and edges. An edge links two vertices together, with or without a direction. In a graph, we have some vertices and edges. An edge links two vertices together, with or without a direction. 1 2 4 3 vertex edge

8 Graph Mathematically, a graph is defined as G=(V,E), Mathematically, a graph is defined as G=(V,E), V is the set of vertices (singular: vertex) V is the set of vertices (singular: vertex) E is the set of edges that connect some of the vertices E is the set of edges that connect some of the vertices For convenience, For convenience, Label vertices with 1, 2, 3, … Label vertices with 1, 2, 3, … Edges can be represented by their two endpoints. Edges can be represented by their two endpoints.

9 Graph Directed/Undirected Graph Directed/Undirected Graph Weighted/Unweighted Graph Weighted/Unweighted Graph Simple Graph Simple Graph Connectivity Connectivity

10 Graph Modelling S E S E

11 How to Store the Graph A V x V array storing whether the i th vertex has an edge to the j th vertex (simple graph only) i.e. d[i][j] = 1 if there is an edge from i to j; if not, d[i][j] = 0 An array storing the endpoints of each edge i.e. edge[i][1] = u and edge[i][2] = v means the i th edge connects u and v

12 VISITING A GRAPH

13 Visiting a Graph When we have an array we use iterations (for-loops) to scan through the elements of the array. How do we scan through all the vertices of a graph? What order do we use? The vertex labels are probably irrelevant here e.g., we may choose to visit a vertex only after discovering an edge to it from its neighbour one of the ways: recursion

14 Using Recursion Strategy: Go as far as you can (if you have not visit there), otherwise, go back and try another way Strategy: Go as far as you can (if you have not visit there), otherwise, go back and try another way

15 Implementation This is known as Depth-First Search (DFS). This is known as Depth-First Search (DFS). 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 Initially all vertices are marked as unvisited

16 08-07-200616 Note the color of the vertices 1. Vertices fall into 3 categories: Unvisited (White) Discovered (Grey) Dead (All reachable vertices from these vertices are discovered) (Black)

17 Finding Shortest Path DFS helps us find a path from one vertex to another. Is the this path found the shortest one between the two vertices? DFS helps us find a path from one vertex to another. Is the this path found the shortest one between the two vertices? How can we find the shortest distance from a vertex to another? How can we find the shortest distance from a vertex to another?

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

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

20 Question Why does BFS work to find the shortest path?

21 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 Initially all vertices except the start vertex are marked as unvisited and the queue contains the start vertex only

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

23 MORE GRAPH PROBLEMS

24 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

25 Topological Sort In short, in a directed graph, In short, in a directed graph, We want to give a order to the vertices We want to give a order to the vertices So that the there is an edge from u to v, u<v So that the there is an edge from u to v, u<v Is it always possible? Is it always possible? Topological Sort: to find such order Topological Sort: to find such order

26 Observation The vertex numbered 1 must have no incoming edge. The vertex numbered 2 must have no incoming edges other than (possibly) one from vertex 1. And so on…

27 How to solve the problem? Find a vertex with no incoming edges. Number it and remove all outgoing edges from it. Repeat the process. Problem: How to implement the above process? Iteration Recursion

28 Finding area Find the area that is reachable from A. A

29 Flood Fill Starting from one vertex, visit (fill) all vertices that are connected, in order to get some information, e.g. area Starting from one vertex, visit (fill) all vertices that are connected, in order to get some information, e.g. area We can use BFS/DFS We can use BFS/DFS Example: Largest Continuous Region (HKOI2003 Senior Q4) Example: Largest Continuous Region (HKOI2003 Senior Q4)

30 MORE ON REPRESENTATION OF GRAPHS

31 Why how to store matters? In BFS/DFS, we perform operations on all neighbours of some vertices. In some other applications, we may check whether there is an edge between two vertices. Time complexities in doing these tasks depend on how we store the graph, thus affecting the runtime of our program.

32 Representation of Graph Adjacency Matrix Adjacency Matrix Adjacency linked list Adjacency linked list Edge list Edge list

33 Adjacency Matrix 123456 1001011 2000000 3100001 4000000 5010000 6000100 1 2 3 4 6 5

34 Adjacency Linked List 1  3  5  6 2 3  1  6 4 5  2 6  4 1 2 3 4 6 5

35 Edge List ie[i][1]e[i][2] 113 215 316 431 536 652 764 1 2 3 4 6 5 is[i] 11 24 34 46 56 67

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

37 Other Topics Euler Path / Circuit Euler Path / Circuit Diameter and Radius Diameter and Radius More advanced topics: More advanced topics: Finding SCC Finding SCC Bidirectional search (BDS) Bidirectional search (BDS) Iterative deepening search (IDS) Iterative deepening search (IDS)

38 1067 Maze 1067 Maze 2045 Teacher’s Problem 2045 Teacher’s Problem 2037 Largest Continuous Region (HKOI 2003 Senior Q4) 2037 Largest Continuous Region (HKOI 2003 Senior Q4) 3021 Bomber Man 3021 Bomber Man Practice Problems


Download ppt "Introduction to Graph Theory HKOI Training (Intermediate) Kelly Choi 29 Mar 2008."

Similar presentations


Ads by Google