+ All Categories
Home > Documents > Introduction to Graph Theory

Introduction to Graph Theory

Date post: 01-Jan-2016
Category:
Upload: imogene-dixon
View: 62 times
Download: 3 times
Share this document with a friend
Description:
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 - PowerPoint PPT Presentation
Popular Tags:
40
Introduction to Graph Introduction to Graph Theory Theory HKOI Training HKOI Training (Intermediate) (Intermediate) Kelly Choi Kelly Choi 28 Mar 2009 28 Mar 2009
Transcript
Page 1: Introduction to Graph Theory

Introduction to Graph TheoryIntroduction to Graph Theory

HKOI Training (Intermediate)HKOI Training (Intermediate)Kelly ChoiKelly Choi

28 Mar 200928 Mar 2009

Page 2: Introduction to Graph Theory

OverviewOverview

Introduction to GraphsIntroduction to Graphs Graph RepresentationGraph Representation Visiting a graphVisiting a graph

BFS (Breadth First Search)BFS (Breadth First Search) DFS (Depth First Search)DFS (Depth First Search)

Topological SortTopological Sort Flood FillFlood Fill Other TopicsOther Topics

Page 3: Introduction to Graph Theory

A Classical ProblemA Classical Problem

MazeMaze Find a path Find a path

from S to Efrom S to E

S

E

Page 4: Introduction to Graph Theory

RumoursRumours

In a group of people, a rumour start In a group of people, a rumour start from from SamSam to others. to others.

Each person can spread the rumour Each person can spread the rumour to his/her friends. to his/her friends.

Through how many persons will the Through how many persons will the rumour reach rumour reach EmilyEmily??

SamSam

EmilyEmily

TimTim

KateKate

JohnJohn

Page 5: Introduction to Graph Theory

Aren’t the two problems similar?Aren’t the two problems similar?

Essentially we have some Essentially we have some verticesvertices which are linked together by some which are linked together by some edgesedges..

In both problems, we want to find a In both problems, we want to find a path to get from one vertex to path to get from one vertex to another.another. In other cases, it could be some other In other cases, it could be some other

problems. problems.

Page 6: Introduction to Graph Theory

WHAT ARE GRAPHS?

Page 7: Introduction to Graph Theory

GraphGraph

In a graph, we have some In a graph, we have some verticesvertices and and edgesedges. An edge links two . An edge links two vertices together, with or without a vertices together, with or without a direction. direction.

1

2

4

3

vertex

edge

Page 8: Introduction to Graph Theory

GraphGraph

Mathematically, a Mathematically, a graphgraph is defined is defined as G=(V,E), 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 E is the set of edges that connect some

of the verticesof 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 Edges can be represented by their two

endpoints.endpoints.

Page 9: Introduction to Graph Theory

GraphGraph

Directed/Undirected GraphDirected/Undirected Graph Weighted/Unweighted GraphWeighted/Unweighted Graph Simple GraphSimple Graph ConnectivityConnectivity

Page 10: Introduction to Graph Theory

Graph Modelling

S

E

SS

EE

Page 11: Introduction to Graph Theory

GRAPH REPRESENTATION

Page 12: Introduction to Graph Theory

Representation of GraphRepresentation of Graph

How do we store a graph in a How do we store a graph in a program?program? Adjacency MatrixAdjacency Matrix Adjacency linked listAdjacency linked list Edge listEdge list

Page 13: Introduction to Graph Theory

Adjacency Matrix

1 2 3 4 5 6

1 0 0 1 0 1 1

2 0 0 0 0 0 0

3 1 0 0 0 0 1

4 0 0 0 0 0 0

5 0 1 0 0 0 0

6 0 0 0 1 0 0

11

2233

44

66

55

Page 14: Introduction to Graph Theory

Adjacency Linked List

1 3 5 6

2

3 1 6

4

5 2

6 4

11

2233

44

66

55

Page 15: Introduction to Graph Theory

Edge List

i e[i][1] e[i][2]

1 1 3

2 1 5

3 1 6

4 3 1

5 3 6

6 5 2

7 6 4

11

2233

44

66

55

i s[i]

1 1

2 4

3 4

4 6

5 6

6 7

Page 16: Introduction to Graph Theory

Representation of GraphRepresentation of Graph

Adjacency Adjacency MatrixMatrix

Adjacency Adjacency Linked ListLinked List

Edge ListEdge List

Memory Memory StorageStorage

O(VO(V22)) O(V+E)O(V+E) O(V+E)O(V+E)

Check Check whether whether ((uu,,vv) is an ) is an edgeedge

O(1)O(1) O(deg(u))O(deg(u)) O(deg(u))O(deg(u))

Find all Find all adjacent adjacent vertices of a vertices of a vertex vertex uu

O(V)O(V) O(deg(u))O(deg(u)) O(deg(u))O(deg(u))

deg(u): the number of edges connecting vertex deg(u): the number of edges connecting vertex uu

Page 17: Introduction to Graph Theory

VISITING A GRAPH

Page 18: Introduction to Graph Theory

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

Page 19: Introduction to Graph Theory

Using RecursionUsing Recursion

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

Page 20: Introduction to Graph Theory

ImplementationImplementation

This is known as Depth-First Search This is known as Depth-First Search (DFS).(DFS).

DFS (vertex DFS (vertex uu) {) {

mark mark uu as as visitedvisited

for each vertex for each vertex vv directly reachable from directly reachable from uu

if if vv is is unvisitedunvisited

DFS (DFS (vv))

}}

Initially all vertices are marked as Initially all vertices are marked as unvisitedunvisited

Page 21: Introduction to Graph Theory

08-07-2006 21

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)

Page 22: Introduction to Graph Theory

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

Page 23: Introduction to Graph Theory

Breadth-First Search (BFS)Breadth-First Search (BFS)

BFS tries to find the target from BFS tries to find the target from nearestnearest vertices first. vertices first.

BFSBFS uses a queue to store discovered uses a queue to store discovered

verticesvertices expand the path from the earliest expand the path from the earliest

discovered vertices.discovered vertices.

Page 24: Introduction to Graph Theory

1

4

3

25

6

Simulation of BFSSimulation of BFS

Queue:Queue: 1 4 3 5 2 6

Page 25: Introduction to Graph Theory

Question

Why does BFS work to find the shortest path?

Page 26: Introduction to Graph Theory

ImplementationImplementation

while queue Q not emptywhile queue Q not emptydequeue the first vertex dequeue the first vertex uu from Q from Qfor each vertex for each vertex vv directly reachable from directly reachable from uu

if if vv is is unvisitedunvisitedenqueue enqueue vv to Q to Qmark mark vv as as visitedvisited

Initially all vertices except the start Initially all vertices except the start vertex are marked as vertex are marked as unvisitedunvisited and and the queue contains the start vertex the queue contains the start vertex onlyonly

Page 27: Introduction to Graph Theory

AdvantagesAdvantages

Guarantee shortest paths for Guarantee shortest paths for unweighted graphsunweighted graphs

Use queue instead of recursive Use queue instead of recursive functions – Avoiding stack overflowfunctions – Avoiding stack overflow

Page 28: Introduction to Graph Theory

MORE GRAPH PROBLEMS

Finding the shortest paths isn’t the only graph problem…

Page 29: Introduction to Graph Theory

Teacher’s Problem (HKOI 2004 Senior)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

Page 30: Introduction to Graph Theory

Topological SortTopological Sort

In short, in a In short, in a directeddirected graph, graph, We want to give a label to the verticesWe want to give a label to the vertices So that if there is an edge from u to v, So that if there is an edge from u to v,

then u<vthen u<v Is it always possible?Is it always possible?

Topological Sort: to find such orderTopological Sort: to find such order

Page 31: Introduction to Graph Theory

ObservationObservation

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…

Page 32: Introduction to Graph Theory

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

Page 33: Introduction to Graph Theory

Finding area

Find the area that is reachable from A. A

Page 34: Introduction to Graph Theory

Flood FillFlood Fill

Starting from one vertex, visit (fill) Starting from one vertex, visit (fill) all vertices that are connected, in all vertices that are connected, in order to get some information, e.g. order to get some information, e.g. areaarea

We can use BFS/DFSWe can use BFS/DFS

Example: Largest Continuous Example: Largest Continuous Region (HKOI2003 Senior Q4)Region (HKOI2003 Senior Q4)

Page 35: Introduction to Graph Theory

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.

Page 36: Introduction to Graph Theory

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

Page 37: Introduction to Graph Theory

Miscellaneous TopicsMiscellaneous Topics

Euler Path / CircuitEuler Path / Circuit A path/circuit that goes through every A path/circuit that goes through every

edge edge exactly onceexactly once Diameter and RadiusDiameter and Radius TreesTrees More advanced topicsMore advanced topics

Finding Strongly Connected Finding Strongly Connected Components (SCC)Components (SCC)

Page 38: Introduction to Graph Theory

Preview of Other Advanced Problems

More on DFS and BFSMore on DFS and BFS Shortest path in a weighted graphShortest path in a weighted graph

Dijkstra’s Algorithm: Using priority Dijkstra’s Algorithm: Using priority queuequeue

Disjoint set and minimum spanning Disjoint set and minimum spanning treestrees

Page 39: Introduction to Graph Theory

Preview of Other Advanced Problems

Searching techniques: Searching techniques: Bidirectional search (BDS) Bidirectional search (BDS) Iterative deepening search (IDS)Iterative deepening search (IDS)

Network Flow (not required in IOI)Network Flow (not required in IOI)

Page 40: Introduction to Graph Theory

1067 Maze1067 Maze 2045 Teacher’s Problem2045 Teacher’s Problem 2037 Largest Continuous Region2037 Largest Continuous Region

(HKOI 2003 Senior Q4)(HKOI 2003 Senior Q4) 2066 Squareland2066 Squareland 3021 Bomber Man3021 Bomber Man

Practice Problems


Recommended