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

Slides:



Advertisements
Similar presentations
Graph Theory, DFS & BFS Kelly Choi What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic.
Advertisements

Graph and String Matching String Matching Problem Given a text string T of length n and a pattern string P of length m, the exact string matching.
Algorithms and Data Structures
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.
Breadth-First and Depth-First Search
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Graph & BFS.
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.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
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.
 Last lesson  Graphs  Today  Graphs (Implementation, Traversal)
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
CS261 Data Structures DFS and BFS – Edge List Representation.
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.
Graph Introduction, Searching Graph Theory Basics - Anil Kishore.
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
© 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.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
Introduction to Algorithms
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Graphs A New Data Structure
Graphs Breadth First Search & Depth First Search
Graph Representation, DFS and BFS
Data Structures Graphs - Terminology
Graphs Representation, BFS, DFS
CSE373: Data Structures & Algorithms Lecture 13: Topological Sort / Graph Traversals Kevin Quinn Fall 2015.
CSC317 Graph algorithms Why bother?
CSE 2331/5331 Topic 9: Basic Graph Alg.
Csc 2720 Instructor: Zhuojun Duan
Graph Searching.
Depth-First Search.
Graphs Breadth First Search & Depth First Search
Unweighted Shortest Path Neil Tang 3/11/2010
CS120 Graphs.
Graph.
Graph Representation, DFS and BFS
Elementary Graph Algorithms
Lecture 10 Algorithm Analysis
Graph & BFS.
Graphs Representation, BFS, DFS
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Search Related Algorithms
Chapter 22: Elementary Graph Algorithms I
Graph Representation (23.1/22.1)
Lectures on Graph Algorithms: searching, testing and sorting
Graphs.
Graphs.
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
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.
Graphs.
Graphs.
Elementary Graph Algorithms
Graphs.
Analysis and design of algorithm
Algorithms CSCI 235, Spring 2019 Lecture 32 Graphs I
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:

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

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

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

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.

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.

WHAT ARE GRAPHS?

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 vertex edge

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.

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

Graph Modelling S E S E

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

VISITING A GRAPH

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

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

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

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)

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?

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.

Simulation of BFS Queue: Queue:

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 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 Guarantee shortest paths for unweighted graphs Use queue instead of recursive functions – Avoiding stack overflow Use queue instead of recursive functions – Avoiding stack overflow

MORE GRAPH PROBLEMS

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, 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

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

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)

MORE ON REPRESENTATION OF GRAPHS

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.

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

Adjacency Matrix

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

Edge List ie[i][1]e[i][2] is[i]

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

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)

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