# graph theory Chapter 9 Graph Algorithms - William & tadavis/cs303/ · PDF...

date post

27-Aug-2018Category

## Documents

view

227download

0

Embed Size (px)

### Transcript of graph theory Chapter 9 Graph Algorithms - William & tadavis/cs303/ · PDF...

Chapter 9Graph Algorithms

Introduction

2

graph theoryuseful in practicerepresent many real-life problemscan be if not careful with data structures

Definitions

3

an undirected graph is a finite set of____________ together with a set of ___________an edge is a pair , where and are vertices

this definition allows____________, or edges that connect vertices tothemselvesparallel edges, or multiple edges that connect the samepair of vertices

a graph without self-loops is a simple grapha graph with parallel edges is sometimes called a multigraph

Definitions

4

two vertices are adjacent if there is an between themthe edge is said to be incident to the two vertices

if there are no parallel edges, the degree of a vertex is thenumber of edges to it

self-loops add only to the degree

a subgraph of a graph is a subset of 's edges togetherwith the incident vertices

Definitions

5

a path in a graph is a sequence of vertices connected by____________

a simple path is a path with no ______________ vertices,except possibly the first and last

a cycle is a path of at least one edge whose first and last___________ are the same

a simple cycle is a cycle with no repeated edges ofvertices other than the first and last

the length of a path is the number of in the path

Definitions

6

a graph is connected if every vertex is connected to everyother vertex by a through the graph

a connected component of a graph is a maximalconnected subgraph of : if is a subset of and is aconnected subgraph of , then

a graph that is not connected consists of a set of connected_______________

a graph without cycles is called acyclic

Definitions

7

a tree is a connected, acyclic grapha forest is a set of treesa spanning tree of a connected graph is a subgraph that is atree and also contains all of the graph's _____________a spanning forest of a graph is the union of spanning treesof its connected components

Definitions

8

if is the number of vertices and is the number ofedges, then, in a graph without self-loops and paralleledges, there are possible ___________a graph is complete if there is an edge between every________ of verticesthe density of a graph refers to the proportion of possiblepairs of vertices that are connecteda sparse graph is one for whicha dense graph is a graph that is not ____________a bipartite graph is one whose vertices can be divided intotwo sets so that every vertex in one set is connected to atleast one vertex in the other set

Definitions

9

in a directed graph or digraph the pairs indicatingedges are : the edge goes from(the tail) to (the head)since edges have a direction, we use the notation todenote an edge from toedges in digraphs are frequently called arcsthe of a vertex is the number of arcs

(i.e., the number of arcs coming into ), while theoutdegree of is the number of arcs (i.e., thenumber of arcs exiting )we will call a source if its indegree is ____an aborescence is a directed graph with a distinguishedvertex (the root) such that for every other vertex there isa unique directed path from to

Definitions

10

in a directed graph, two vertices and are ____________connected if there is a directed path from to and adirected path from toa digraph is strongly connected if all its vertices are stronglyconnectedif a digraph is not strongly connected but the underlyingundirected graph is connected, then the digraph is calledweakly connecteda weighted graph has weights or costs associated with each___________

weighted graphs can be directed or undirecteda road map with mileage is the prototypical example

Example

11

airport connections

http://allthingsgraphed.com/2014/08/09/us-airlines-graph/

Graph Representation

12

two concerns: and ___________

similarthe following graph has 7 vertices and 12 edges

Graph Representation

13

adjacency matrix2D matrix where an element is 1 if and 0otherwise

Graph Representation

14

adjacency matrixalternatively, could use costs or for _____________not efficient if the graph is (number of edges small)

matrixe.g., street map with 3,000 streets results in intersectionmatrix with 9,000,000 elements

adjacency liststandard way to represent graphsundirected graph edges appear in listmore efficient if the graph is sparse (number of edgessmall)

matrix

Graph Representation

15

adjacency listfor each vertex, keep a list of vertices

Graph Representation

16

adjacency list alternativefor each vertex, keep a of adjacent vertices

Topological Sort

17

a directed acyclic graph (DAG) is a digraph with no directedcycles

a DAG always has at least one __________

topological sortan ordering of the vertices in a directed graph such that ifthere is a path from v to w, then v appears w inthe orderingnot possible if graph has a ___________

Topological Sort

18

example directed acyclic graph

Topological Sort

19

topological sortdetermine the indegree for every

place all source vertices in a queuewhile there remains a

find a vertexappend the source vertex to the topological sortdelete the source and its adjacent fromupdate the of the remaining vertices inplace any new source vertices in the queue

when no vertices remain, we have our ordering, or, if we aremissing vertices from the output list, the graph has notopological sort

Topological Sort

20

since finding vertex with 0 indegree must look at _______vertices, and this is performed times,

Topological Sort

21

instead, we can keepall the vertices withindegree 0 in a ______and choose from there

Topological Sort

22

adjacency list alternativefor each , keep a vector of adjacent vertices

Shortest-Path Algorithms

23

shortest-path problemsinput is a weighted graph with a on each edgeweighted path length:

single-source shortest-path problemgiven as input a weighted graph, and a_______________ vertex , find the shortest weightedpath from to every other vertex in

Shortest-Path Algorithms

24

exampleshortest weighted path from to has cost of _____no path from to

Shortest-Path Algorithms

25

edges can cause problemspath from to has cost of 1, but a shorter path exists byfollowing the negative loop, which has cost -5shortest paths thus _______________

Shortest-Path Algorithms

26

many examples where we want to find shortest pathsif vertices represent computers and edges connections, thecost represents costs, delay costs, orcombination of costsif vertices represent airports and edges costs to travelbetween them, shortest path is route

we find paths from one vertex to others since noalgorithm exists that finds shortest path from one vertex toone other faster

Shortest-Path Algorithms

27

four problemsunweighted shortest-pathweighted shortest-path with no negative edgesweighted shortest-path with negative edgesweighted shortest-path in acyclic graphs

Unweighted Shortest Paths

28

unweighted shortest-pathfind shortest paths from to all other verticesonly concerned with number of in path

we will not actually record the path ________________

Unweighted Shortest Paths

29

examplestart with

Unweighted Shortest Paths

30

examplemark 0 length to

Unweighted Shortest Paths

31

examplemark 1 length for and

Unweighted Shortest Paths

32

examplemark 2 length for and

Unweighted Shortest Paths

33

examplefinal path assignments

Unweighted Shortest Paths

34

searching an unweighted shortest-path uses a_________________ search

processes vertices in layers, according to _____________begins with initializing path lengths

a vertex will be marked when the shortest pathto it is found

Unweighted Shortest Paths

35

Unweighted Shortest Paths

36

with this algorithmpath can be printedrunning time:bad case

can reduce time by keeping vertices that are unknown__________________ from those knownnew running time:

Unweighted Shortest Paths

37

Unweighted Shortest Paths

38

Algorithm

39

weighted shortest-path algorithmmore difficult, but ideas from algorithmcan be usedkeep information as before for each vertex

knownset if using onlyknown vertices

the last vertex to cause a change toalgorithm

does what appears to be best thing at each stagee.g., counting money: count quarters first, then dimes,nickels, pennies

gives change with least number of coins

Algorithm

40

pseudocodeassumption: no weightsorigin is given

Algorithm

41

pseudocode (cont.)

Algorithm

42

example: start at

Algorithm

43

example

Algorithm

44

example

,

Algorithm

45

example stages shown on the graph

Algorithm

46

example stages shown on the graph (cont.)

Algorithm: Correctness

47

correctness

Algorithm

48

complexitysequentially scanning vertices to find minimum takes

, which results in overallat most one update per edge, for a total of

if graph is dense, with , algorithm is close to________________if graph is sparse, with , algorithm is too__________

distances could be kept in a queue thatreduces running time to

Algorithm

49

implementationinformation for each vertex

Algorithm

50

implementation (cont.)path can be p