1 / 40

Introduction to Graph Theory

Introduction to Graph Theory. HKOI Training (Intermediate) Kelly Choi 28 Mar 2009. Overview. Introduction to Graphs Graph Representation Visiting a graph BFS (Breadth First Search) DFS (Depth First Search) Topological Sort Flood Fill Other Topics. A Classical Problem. Maze

Download Presentation

Introduction to Graph Theory

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Graph Theory HKOI Training (Intermediate) Kelly Choi 28 Mar 2009

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

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

  4. Rumours In a group of people, a rumour start from Sam to others. Each person can spread the rumour to his/her friends. Through how many persons will the rumour reach Emily? John Sam Tim Emily Kate

  5. Aren’t the two problems similar? Essentially we have some vertices which are linked together by some edges. In both problems, we want to find a path to get from one vertex to another. In other cases, it could be some other problems.

  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. 1 4 vertex 2 edge 3

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

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

  10. Graph Modelling S E

  11. GRAPH REPRESENTATION

  12. Representation of Graph How do we store a graph in a program? Adjacency Matrix Adjacency linked list Edge list

  13. Adjacency Matrix 1 5 6 2 3 4

  14. Adjacency Linked List 1 5 6 2 3 4

  15. Edge List 1 5 6 2 3 4

  16. Representation of Graph

  17. VISITING A GRAPH

  18. Visiting a Graph To scan an array: use iterations (for-loops) How to scan the vertices of a graph? e.g. to find a path from S to E Usually we visit a vertex only after discovering an edge to it from its neighbour one of the ways: recursion

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

  20. Implementation 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

  21. Note the color of the vertices Vertices fall into 3 categories: Unvisited (White) Discovered (Grey) Dead (All reachable vertices from these vertices are discovered) (Black)

  22. Use of DFS • DFS is very useful • When you have to do something with every vertices • When you want to know whether one vertex is connected to another vertices • Drawbacks • Stack overflow • Finding a shortest path is difficult

  23. Breadth-First Search (BFS) BFS tries to find the target from nearest vertices first. BFS uses a queue to store discovered vertices expand the path from the earliest discovered vertices.

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

  25. Question Why does BFS work to find the shortest path?

  26. 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

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

  28. MORE GRAPH PROBLEMS Finding the shortest paths isn’t the only graph problem…

  29. 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

  30. Topological Sort 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? Topological Sort: to find such order

  31. 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…

  32. 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

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

  34. Flood Fill 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 Example: Largest Continuous Region (HKOI2003 Senior Q4)

  35. Remark on representation of graphs • In BFS/DFS, we perform operations on all neighbours of some vertices. • Use adjacency linked list / edge list • In some other applications, we may check whether there is an edge between two vertices. • Adjacency matrix may be better in some cases.

  36. Summary • You should know • What a graph is • What it means to model a problem with a graph • Basic ideas and implementation of DFS/BFS to find shortest paths / scan all vertices • Some ideas of Topological Sort and Floodfilling

  37. Miscellaneous Topics • Euler Path / Circuit • A path/circuit that goes through every edge exactly once • Diameter and Radius • Trees • More advanced topics • Finding Strongly Connected Components (SCC)

  38. Preview of Other Advanced Problems • More on DFS and BFS • Shortest path in a weighted graph • Dijkstra’s Algorithm: Using priority queue • Disjoint set and minimum spanning trees

  39. Preview of Other Advanced Problems • Searching techniques: • Bidirectional search (BDS) • Iterative deepening search (IDS) • Network Flow (not required in IOI)

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

More Related