algorithms and data structures - graphs - bfh-ti staff: …hnr1/sws/09graphs.pdf ·  ·...

27
Graphs Page 1 BFH-TI: Softwareschule Schweiz Algorithms and Data Structures Graphs Dr. Rolf Haenni CAS SD01 Berner Fachhochschule Rolf Haenni Technik und Informatik Algorithms and Data Structures

Upload: phunghuong

Post on 21-May-2018

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Algorithms and Data Structures - Graphs - BFH-TI Staff: …hnr1/SWS/09Graphs.pdf ·  · 2008-11-25Algorithms and Data Structures Graphs Dr. Rolf Haenni CAS SD01 ... c g j d i I U

Graphs Page 1

BFH-TI: Softwareschule Schweiz

Algorithms and Data Structures

Graphs

Dr. Rolf Haenni

CAS SD01

Berner Fachhochschule Rolf Haenni

Technik und Informatik Algorithms and Data Structures

Page 2: Algorithms and Data Structures - Graphs - BFH-TI Staff: …hnr1/SWS/09Graphs.pdf ·  · 2008-11-25Algorithms and Data Structures Graphs Dr. Rolf Haenni CAS SD01 ... c g j d i I U

Graphs Page 2

Outline

Graphs

Implementing Graphs

Graph Algorithms

Berner Fachhochschule Rolf Haenni

Technik und Informatik Algorithms and Data Structures

Page 3: Algorithms and Data Structures - Graphs - BFH-TI Staff: …hnr1/SWS/09Graphs.pdf ·  · 2008-11-25Algorithms and Data Structures Graphs Dr. Rolf Haenni CAS SD01 ... c g j d i I U

Graphs Page 3Graphs

Outline

Graphs

Implementing Graphs

Graph Algorithms

Berner Fachhochschule Rolf Haenni

Technik und Informatik Algorithms and Data Structures

Page 4: Algorithms and Data Structures - Graphs - BFH-TI Staff: …hnr1/SWS/09Graphs.pdf ·  · 2008-11-25Algorithms and Data Structures Graphs Dr. Rolf Haenni CAS SD01 ... c g j d i I U

Graphs Page 4Graphs

What is a Graph?

I In mathematics and computer science, a graph is a pairG = (V , E ) where

Ý V is a set of n vertices (nodes)Ý E is a collection of m edges

I Vertices and edges are positions, i.e. they store elements

I Example: Flight network

HNL

SFO

LAX

ORD

DFW

LGA

PVD

MIA

1344

2555 1233

1755344

1873844

801 145

10991120

Berner Fachhochschule Rolf Haenni

Technik und Informatik Algorithms and Data Structures

Page 5: Algorithms and Data Structures - Graphs - BFH-TI Staff: …hnr1/SWS/09Graphs.pdf ·  · 2008-11-25Algorithms and Data Structures Graphs Dr. Rolf Haenni CAS SD01 ... c g j d i I U

Graphs Page 5Graphs

Undirected vs. Directed Graphs

I There are two different types of edges

Ý Undirected edges e = {v , w}Ý Directed edges e = (v , w), sometimes called arcs

SFO ORD1873miles SFO ORDflight

AA101

I Hence, there are three different types of graphs

Ý Undirected graphs consist of undirected edges onlyÝ Directed graphs consist of directed edges onlyÝ Mixed graphs consist of both directed and undirected edges

I An undirected edge {v , w} can be replaced by two oppositedirected edges (v , w) and (w , v)

Berner Fachhochschule Rolf Haenni

Technik und Informatik Algorithms and Data Structures

Page 6: Algorithms and Data Structures - Graphs - BFH-TI Staff: …hnr1/SWS/09Graphs.pdf ·  · 2008-11-25Algorithms and Data Structures Graphs Dr. Rolf Haenni CAS SD01 ... c g j d i I U

Graphs Page 6Graphs

Terminology

I General graphs

Ý v and w are adjacent, if G contains an edge {v , w} or (v , w)Ý v and w are endpoints of {v , w} or (v , w)Ý The degree of a vertex v is the number of edges {v , w},

(v , w), and (w , v) in GÝ A self-loop is an edge {v , v} or (v , v)Ý Two edges e1 and e2 are called parallel if e1 = e2

I A simple graph is free of self-loops and parallel edgesI Directed graphs

Ý v is the origin and w is the destination of (v , w)Ý (v , w) is an outgoing edge of v and an incoming edge of wÝ The out-degree of v is the number of outgoing edges of v in GÝ The in-degree of v is the number of incoming edges of v in G

Berner Fachhochschule Rolf Haenni

Technik und Informatik Algorithms and Data Structures

Page 7: Algorithms and Data Structures - Graphs - BFH-TI Staff: …hnr1/SWS/09Graphs.pdf ·  · 2008-11-25Algorithms and Data Structures Graphs Dr. Rolf Haenni CAS SD01 ... c g j d i I U

Graphs Page 7Graphs

Terminology: Example

U

W

V

X

Y

Z

h

a f

eb

c

g

j

di

I U and V are adjacent

Ý U and V are the endpoints of aÝ U is the origin and V the destination of a

I The degree of V is 4

Ý The indegree of V is 2 (a and e are incoming edges)Ý The outdegree of V is 2 (b and f are outgoing edges)

I j is a self-loop, c and d are parallel

Berner Fachhochschule Rolf Haenni

Technik und Informatik Algorithms and Data Structures

Page 8: Algorithms and Data Structures - Graphs - BFH-TI Staff: …hnr1/SWS/09Graphs.pdf ·  · 2008-11-25Algorithms and Data Structures Graphs Dr. Rolf Haenni CAS SD01 ... c g j d i I U

Graphs Page 8Graphs

Paths and Cycles

I A path is sequence of alternating vertices and edges

Ý Begins with a verticeÝ Ends with a verticeÝ Each edge is preceded and followed by its endpoints

I A cycle is circular sequence of alternating vertices and edges

Ý Each edge is preceded and followed by its endpoints

I A path or cycle is called simple, if all its vertices and edges aredistinct

I A graph is called unrooted tree, if it contains no cycles

I A graph is called connected, if there is a path between anypair of vertices

Berner Fachhochschule Rolf Haenni

Technik und Informatik Algorithms and Data Structures

Page 9: Algorithms and Data Structures - Graphs - BFH-TI Staff: …hnr1/SWS/09Graphs.pdf ·  · 2008-11-25Algorithms and Data Structures Graphs Dr. Rolf Haenni CAS SD01 ... c g j d i I U

Graphs Page 9Graphs

Terminology: Example

U

W

V

X

Y

Z

h

a f

ebc

g

j

di

I U, a, V , f , Y , g , X , e, V , b, W is a path that is not simple

I X , i , Z , h, Y , g , X , i , Z , . . . is a simple cycle

I The graph is not a tree

I The graph is connected

Berner Fachhochschule Rolf Haenni

Technik und Informatik Algorithms and Data Structures

Page 10: Algorithms and Data Structures - Graphs - BFH-TI Staff: …hnr1/SWS/09Graphs.pdf ·  · 2008-11-25Algorithms and Data Structures Graphs Dr. Rolf Haenni CAS SD01 ... c g j d i I U

Graphs Page 10Graphs

Graph ADT

I Vertices and edges are positions which store elements

I Edge operations

Ý endVertices(e)Ý isDirected(e)Ý origin(e)Ý destination(e)

I Vertex operations

Ý aVertex()Ý degree(v)Ý inDegree(v)Ý outDegree(v)Ý incidentEdges(v)Ý areAdjacent(v,w)

I General operations

Ý numVertices()Ý numEdges()Ý vertices(v)Ý edges(v)

I Update operations

Ý insertVertex(elem)Ý insertEdge(v,w,elem)Ý insertArc(v,w,elem)Ý removeVertex(v)Ý removeEdge(e)

Berner Fachhochschule Rolf Haenni

Technik und Informatik Algorithms and Data Structures

Page 11: Algorithms and Data Structures - Graphs - BFH-TI Staff: …hnr1/SWS/09Graphs.pdf ·  · 2008-11-25Algorithms and Data Structures Graphs Dr. Rolf Haenni CAS SD01 ... c g j d i I U

Graphs Page 11Implementing Graphs

Outline

Graphs

Implementing Graphs

Graph Algorithms

Berner Fachhochschule Rolf Haenni

Technik und Informatik Algorithms and Data Structures

Page 12: Algorithms and Data Structures - Graphs - BFH-TI Staff: …hnr1/SWS/09Graphs.pdf ·  · 2008-11-25Algorithms and Data Structures Graphs Dr. Rolf Haenni CAS SD01 ... c g j d i I U

Graphs Page 12Implementing Graphs

Edge List Structure

I This representation maintains two sequences (or dictionaries),one for the edges and one for the vertices of the graphs

I Each edge object consists of:

Ý ElementÝ Reference to first endpoint (origin)Ý Reference to second endpoint (destination)

I The edges are connected to the vertices, but not vice versa

a b cU

W

Va

b

c

U V W

E

V

Berner Fachhochschule Rolf Haenni

Technik und Informatik Algorithms and Data Structures

Page 13: Algorithms and Data Structures - Graphs - BFH-TI Staff: …hnr1/SWS/09Graphs.pdf ·  · 2008-11-25Algorithms and Data Structures Graphs Dr. Rolf Haenni CAS SD01 ... c g j d i I U

Graphs Page 13Implementing Graphs

Adjacency List Structure

I Extends the edge list structure by linking the vertices back tothe edges

I Each vertex object consists of:

Ý ElementÝ Sequence of incident edges in which it appears

I Regards the edge-vertex relations from both sides

U

W

Va

b

c

a b c

U V W

E

V

Berner Fachhochschule Rolf Haenni

Technik und Informatik Algorithms and Data Structures

Page 14: Algorithms and Data Structures - Graphs - BFH-TI Staff: …hnr1/SWS/09Graphs.pdf ·  · 2008-11-25Algorithms and Data Structures Graphs Dr. Rolf Haenni CAS SD01 ... c g j d i I U

Graphs Page 14Implementing Graphs

Adjacency Matrix Structure

I The vertices of a simple graph are numbered from 0, . . . , n− 1

I Vertex i is stored at rank i (vector) or with key i (dictionary)I Edges are stored in a 2-dimensional array A[i , j ] of size n × n

Ý A directed edge (i , j) is stored at A[i , j ]Ý An undirected edge {i , j} is stored at A[i , j ] and A[j , i ]Ý All other array cells refer to NULL

I Edges should contain references to its endpoints

I In graph algorithms, adjacency matrices are often reduced tosimple 0, 1-matrices

A[i , j ] =

{1, if (i , j) or {0, 1} is an edge

0, otherwise

Berner Fachhochschule Rolf Haenni

Technik und Informatik Algorithms and Data Structures

Page 15: Algorithms and Data Structures - Graphs - BFH-TI Staff: …hnr1/SWS/09Graphs.pdf ·  · 2008-11-25Algorithms and Data Structures Graphs Dr. Rolf Haenni CAS SD01 ... c g j d i I U

Graphs Page 15Implementing Graphs

Adjacency Matrix Structure: Example

U

W

V

X

Y

Z

h

a f

eb

c

g

j

di

Key Vertex0 U1 V2 W3 X4 Y5 Z

0 1 2 3 4 50 a1 b f2 c3 e d i4 g5 h j

0 1 2 3 4 50 0 1 0 0 0 01 0 0 1 0 1 02 0 0 0 1 0 03 0 1 1 0 0 14 0 0 0 1 0 05 0 0 0 0 1 1

Berner Fachhochschule Rolf Haenni

Technik und Informatik Algorithms and Data Structures

Page 16: Algorithms and Data Structures - Graphs - BFH-TI Staff: …hnr1/SWS/09Graphs.pdf ·  · 2008-11-25Algorithms and Data Structures Graphs Dr. Rolf Haenni CAS SD01 ... c g j d i I U

Graphs Page 16Implementing Graphs

Performance

Edge Adjacency AdjacencyGraph Operation List List Matrix

vertices() n n nedges() m m n2

incidentEdges(v) m deg(v) nareAdjacent(v,w) m min(deg(v), deg(w)) 1insertVertex(elem) 1 1 n2 ∗

removeVertex(v) m deg(v) n2 ∗∗

insertEdge(elem) 1 1 1removeEdge(e) 1 1 1

Space n + m n + m n2

∗ O(1) if array size N > n ∗∗ O(n) if N > n is allowed

Berner Fachhochschule Rolf Haenni

Technik und Informatik Algorithms and Data Structures

Page 17: Algorithms and Data Structures - Graphs - BFH-TI Staff: …hnr1/SWS/09Graphs.pdf ·  · 2008-11-25Algorithms and Data Structures Graphs Dr. Rolf Haenni CAS SD01 ... c g j d i I U

Graphs Page 17Graph Algorithms

Outline

Graphs

Implementing Graphs

Graph Algorithms

Berner Fachhochschule Rolf Haenni

Technik und Informatik Algorithms and Data Structures

Page 18: Algorithms and Data Structures - Graphs - BFH-TI Staff: …hnr1/SWS/09Graphs.pdf ·  · 2008-11-25Algorithms and Data Structures Graphs Dr. Rolf Haenni CAS SD01 ... c g j d i I U

Graphs Page 18Graph Algorithms

General Graph Problems

Graph Traversal: Visit all vertices and edges

Connectivity: Determine whether a graph is connected

Cycle Finding: Find a cycle if one exists

Path Finding: Find a path between two vertices (e.g. theshortest one)

Minimum Cut: Determine the minimal set of edges which, ifremoved, disconnect two vertices

Spanning Tree: Find a connected acyclic subgraph whichcontains all vertices (e.g. the smallest one)

Reachability: Determine whether there is a directed pathbetween two vertices in a digraph

Berner Fachhochschule Rolf Haenni

Technik und Informatik Algorithms and Data Structures

Page 19: Algorithms and Data Structures - Graphs - BFH-TI Staff: …hnr1/SWS/09Graphs.pdf ·  · 2008-11-25Algorithms and Data Structures Graphs Dr. Rolf Haenni CAS SD01 ... c g j d i I U

Graphs Page 19Graph Algorithms

Depth-First-Traversal

I Many graph problems can be solved by traversing the graph

I In a depth-first-traversal, the graph is traversed by exploringpaths as far as possible along each branch before backtracking

I Backtracking means to return to the most recent vertex thathadn’t finished exploring its branches

I Backtracking can be realized in two ways:

Ý Recursion: visit each unexplored adjacent vertex recursivelyÝ Iteration: Push unexplored adjacent vertices to a stack

I In both cases, it is necessary to mark the vertices as VISITED

I Runs in O(n+m) time and uses O(h) space, where h is thelength of the longest simple path

Berner Fachhochschule Rolf Haenni

Technik und Informatik Algorithms and Data Structures

Page 20: Algorithms and Data Structures - Graphs - BFH-TI Staff: …hnr1/SWS/09Graphs.pdf ·  · 2008-11-25Algorithms and Data Structures Graphs Dr. Rolf Haenni CAS SD01 ... c g j d i I U

Graphs Page 20Graph Algorithms

Depth-First-Search: Example

v

v

v v v

v v

v v

v v v

v v

v v v

v v v

v v

v v v

v v v

v v v

v v v

v v v

Berner Fachhochschule Rolf Haenni

Technik und Informatik Algorithms and Data Structures

Page 21: Algorithms and Data Structures - Graphs - BFH-TI Staff: …hnr1/SWS/09Graphs.pdf ·  · 2008-11-25Algorithms and Data Structures Graphs Dr. Rolf Haenni CAS SD01 ... c g j d i I U

Graphs Page 21Graph Algorithms

Breadth-First-Traversal

I In a breadth-first-traversal, all adjacent vertices are visitedimmediately after visiting a vertex

I It is usually implemented by inserting the adjacent verticesinto a queue (starting with an arbitrary node)

I Explored vertices are marked as VISITED

I Can be turned into depth-first-traversal by replacing thequeue by a stack

I Runs in O(n+m) time and uses O(n+m) space in the worstcase

Berner Fachhochschule Rolf Haenni

Technik und Informatik Algorithms and Data Structures

Page 22: Algorithms and Data Structures - Graphs - BFH-TI Staff: …hnr1/SWS/09Graphs.pdf ·  · 2008-11-25Algorithms and Data Structures Graphs Dr. Rolf Haenni CAS SD01 ... c g j d i I U

Graphs Page 22Graph Algorithms

Breadth-First-Search: Example

v v v

v

v v

v v

v

v v

v v v

v v

v v v

v v v

v v v

Berner Fachhochschule Rolf Haenni

Technik und Informatik Algorithms and Data Structures

Page 23: Algorithms and Data Structures - Graphs - BFH-TI Staff: …hnr1/SWS/09Graphs.pdf ·  · 2008-11-25Algorithms and Data Structures Graphs Dr. Rolf Haenni CAS SD01 ... c g j d i I U

Graphs Page 23Graph Algorithms

Weighted Graph Problems

I In a weighted graph, a weight w(e) is attributed to each edgeI Shortest Path: Find the path between two vertices with

minimal total weight

Ý Dijkstra-Algorithm (for positive weights): O(n2) or O(m log n)Ý A*-Algorithm (for positive weights): depends on heuristicÝ Bellman-Ford-Algorithm (for general weights): O(n·m)

I Minimum Spanning Tree: Find a spanning tree with minimaltotal weight

Ý Kruskal-Algorithm: O(m log n)Ý Prim-Algorithm: O(m log n) or O(m + n log n)Ý Baruvka-Algorithm: O(m log n)

I Weighted graphs have applications in transportation networks,planning problems, routing protocols, strategic games, etc.

Berner Fachhochschule Rolf Haenni

Technik und Informatik Algorithms and Data Structures

Page 24: Algorithms and Data Structures - Graphs - BFH-TI Staff: …hnr1/SWS/09Graphs.pdf ·  · 2008-11-25Algorithms and Data Structures Graphs Dr. Rolf Haenni CAS SD01 ... c g j d i I U

Graphs Page 24Graph Algorithms

Dijkstra-Algorithm

I The Dijkstra-Algorithm starts from a source vertex s andcomputes the minimal distances (lengths of the shortestpaths) to all other vertices

I The algorithm is a graph traversal implemented with a priorityqueue, in which the vertices with the shortest distance to sare explored first

I The running time depends on the implementation of thepriority queue

Ý Sequence-based (sorted or unsorted): O(n2)Ý Heap-based: O(m log n)

I Applied in routing protocols such as IS-IS and OSPF

Berner Fachhochschule Rolf Haenni

Technik und Informatik Algorithms and Data Structures

Page 25: Algorithms and Data Structures - Graphs - BFH-TI Staff: …hnr1/SWS/09Graphs.pdf ·  · 2008-11-25Algorithms and Data Structures Graphs Dr. Rolf Haenni CAS SD01 ... c g j d i I U

Graphs Page 25Graph Algorithms

Dijkstra-Algorithm: Example

0 4

2

4

1

7

23

1

1

2

32

0 4

2 9

3

4

1

7

23

1

1

2

32

0 4

2 9

3 5

4

1

7

23

1

1

2

32

0 4

2 8 6

3 5 7

4

1

7

23

1

12

32

0 4 9

2 7 6

3 5 7

4

1

7

23

1

12

32

0 4

1

7

23

1

1

2

32

s s s

s s s

Berner Fachhochschule Rolf Haenni

Technik und Informatik Algorithms and Data Structures

Page 26: Algorithms and Data Structures - Graphs - BFH-TI Staff: …hnr1/SWS/09Graphs.pdf ·  · 2008-11-25Algorithms and Data Structures Graphs Dr. Rolf Haenni CAS SD01 ... c g j d i I U

Graphs Page 26Graph Algorithms

Flow Network Problems

I A flow network is a directed graph with a capacity c(e)attributed to each edge

I Maximum Flow: Find the maximal total flow capacity fbetween two vertices

Ý Ford-Fulkerson-Algorithm: O(m·f ), for f ∈ NÝ Edmonds-Karp-Algorithm: O(n·m2)

I Minimum Cut: Find a cut with minimal total capacitybetween two vertices

I Flow networks have many applications in computer networks,transportation networks, electrical distribution systems, etc.

Berner Fachhochschule Rolf Haenni

Technik und Informatik Algorithms and Data Structures

Page 27: Algorithms and Data Structures - Graphs - BFH-TI Staff: …hnr1/SWS/09Graphs.pdf ·  · 2008-11-25Algorithms and Data Structures Graphs Dr. Rolf Haenni CAS SD01 ... c g j d i I U

Graphs Page 27Graph Algorithms

Maximum Flow: Example

3

5

1

32

3

12

85

s t 0/3

3/5

1/1

3/32/2

3/3

1/10/2

4/84/5

s t

Berner Fachhochschule Rolf Haenni

Technik und Informatik Algorithms and Data Structures