400 likes | 584 Views
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
E N D
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 Find a path from S to E
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
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.
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
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.
Graph Directed/Undirected Graph Weighted/Unweighted Graph Simple Graph Connectivity
Graph Modelling S E
Representation of Graph How do we store a graph in a program? Adjacency Matrix Adjacency linked list Edge list
Adjacency Matrix 1 5 6 2 3 4
Adjacency Linked List 1 5 6 2 3 4
Edge List 1 5 6 2 3 4
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
Using Recursion Strategy: Go as far as you can (if you have not visit there), otherwise, go back and try another way
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
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)
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
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.
Simulation of BFS Queue: 4 1 3 6 2 5 6 1 4 3 5 2
Question Why does BFS work to find the shortest path?
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
MORE GRAPH PROBLEMS Finding the shortest paths isn’t the only graph problem…
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
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
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…
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
Finding area Find the area that is reachable from A.
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)
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.
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
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)
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
Preview of Other Advanced Problems • Searching techniques: • Bidirectional search (BDS) • Iterative deepening search (IDS) • Network Flow (not required in IOI)
1067 Maze 2045 Teacher’s Problem 2037 Largest Continuous Region(HKOI 2003 Senior Q4) 2066 Squareland 3021 Bomber Man Practice Problems