24
Chapter 9 Graph Algorithms Introduction 2 graph theory useful in practice represent many real-life problems can 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 to themselves parallel edges, or multiple edges that connect the same pair of vertices a graph without self-loops is a simple graph a graph with parallel edges is sometimes called a multigraph Definitions 4 two vertices are adjacent if there is an between them the edge is said to be incident to the two vertices if there are no parallel edges, the degree of a vertex is the number of edges to it self-loops add only to the degree a subgraph of a graph is a subset of 's edges together with the incident vertices

graph theory Chapter 9 Graph Algorithms - William & …tadavis/cs303/ch09sm.pdf · Chapter 9 Graph Algorithms Introduction 2 graph theory ... 9 in a directed graph ... A. Church and

Embed Size (px)

Citation preview

Page 1: graph theory Chapter 9 Graph Algorithms - William & …tadavis/cs303/ch09sm.pdf · Chapter 9 Graph Algorithms Introduction 2 graph theory ... 9 in a directed graph ... A. Church and

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

Page 2: graph theory Chapter 9 Graph Algorithms - William & …tadavis/cs303/ch09sm.pdf · Chapter 9 Graph Algorithms Introduction 2 graph theory ... 9 in a directed graph ... A. Church and

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

Page 3: graph theory Chapter 9 Graph Algorithms - William & …tadavis/cs303/ch09sm.pdf · Chapter 9 Graph Algorithms Introduction 2 graph theory ... 9 in a directed graph ... A. Church and

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

Page 4: graph theory Chapter 9 Graph Algorithms - William & …tadavis/cs303/ch09sm.pdf · Chapter 9 Graph Algorithms Introduction 2 graph theory ... 9 in a directed graph ... A. Church and

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

Page 5: graph theory Chapter 9 Graph Algorithms - William & …tadavis/cs303/ch09sm.pdf · Chapter 9 Graph Algorithms Introduction 2 graph theory ... 9 in a directed graph ... A. Church and

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,

Page 6: graph theory Chapter 9 Graph Algorithms - William & …tadavis/cs303/ch09sm.pdf · Chapter 9 Graph Algorithms Introduction 2 graph theory ... 9 in a directed graph ... A. Church and

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

Page 7: graph theory Chapter 9 Graph Algorithms - William & …tadavis/cs303/ch09sm.pdf · Chapter 9 Graph Algorithms Introduction 2 graph theory ... 9 in a directed graph ... A. Church and

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 ________________

Page 8: graph theory Chapter 9 Graph Algorithms - William & …tadavis/cs303/ch09sm.pdf · Chapter 9 Graph Algorithms Introduction 2 graph theory ... 9 in a directed graph ... A. Church and

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

Page 9: graph theory Chapter 9 Graph Algorithms - William & …tadavis/cs303/ch09sm.pdf · Chapter 9 Graph Algorithms Introduction 2 graph theory ... 9 in a directed graph ... A. Church 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:

Page 10: graph theory Chapter 9 Graph Algorithms - William & …tadavis/cs303/ch09sm.pdf · Chapter 9 Graph Algorithms Introduction 2 graph theory ... 9 in a directed graph ... A. Church and

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

Page 11: graph theory Chapter 9 Graph Algorithms - William & …tadavis/cs303/ch09sm.pdf · Chapter 9 Graph Algorithms Introduction 2 graph theory ... 9 in a directed graph ... A. Church and

Algorithm

41

pseudocode (cont.)

Algorithm

42

example: start at

Algorithm

43

example

Algorithm

44

example

,

Page 12: graph theory Chapter 9 Graph Algorithms - William & …tadavis/cs303/ch09sm.pdf · Chapter 9 Graph Algorithms Introduction 2 graph theory ... 9 in a directed graph ... A. Church and

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

Page 13: graph theory Chapter 9 Graph Algorithms - William & …tadavis/cs303/ch09sm.pdf · Chapter 9 Graph Algorithms Introduction 2 graph theory ... 9 in a directed graph ... A. Church and

Algorithm

49

implementationinformation for each vertex

Algorithm

50

implementation (cont.)path can be printed recursively

Algorithm

51

implementation (cont.)

Graphs with Negative Edges

52

try to apply algorithm to graph with edges

Page 14: graph theory Chapter 9 Graph Algorithms - William & …tadavis/cs303/ch09sm.pdf · Chapter 9 Graph Algorithms Introduction 2 graph theory ... 9 in a directed graph ... A. Church and

Graphs with Negative Edges

53

possible solution: a delta value to all weights suchthat none are negative

calculate path on new graphapply path to original graphdoes not work: longer paths become weightier

combination of algorithms for weighted graphs andunweighted graphs can work

drastic in running time:

All-Pairs Shortest Paths

54

given a weighted digraph, find the shortest paths between_____ vertices in the graphone approach: apply Dijkstra's algorithm ________________

results inanother approach: apply Floyd-Warshall algorithm

uses programmingalso results in

Minimum Spanning Tree

55

assumptionsgraph is ______________edge weights are not necessarily Euclidean distancesedge weights need not be all the sameedge weights may be zero or ______________

minimum spanning tree (MST)also called minimum-weight spanning tree of a weightedgraphspanning tree whose weight (the sum of the weights of theedges in the tree) is the among all spanningtrees

Minimum Spanning Tree

56

example

Page 15: graph theory Chapter 9 Graph Algorithms - William & …tadavis/cs303/ch09sm.pdf · Chapter 9 Graph Algorithms Introduction 2 graph theory ... 9 in a directed graph ... A. Church and

Minimum Spanning Tree

57

two algorithms to find the minimum spanning tree

R. C. Prim, Shortest Connection Networks and SomeGeneralizations, Bell System Technical Journal (1957)

AlgorithmJ. B. Kruskal, Jr., On the Shortest Spanning Subtree of aGraph and the Traveling Salesman Problem, Proc. of theAmerican Mathematical Society (1956)

Minimum Spanning Tree

58

grows tree in successive stages

Minimum Spanning Tree

59

Minimum Spanning Tree

60

Page 16: graph theory Chapter 9 Graph Algorithms - William & …tadavis/cs303/ch09sm.pdf · Chapter 9 Graph Algorithms Introduction 2 graph theory ... 9 in a directed graph ... A. Church and

Minimum Spanning Tree

61

Minimum Spanning Tree

62

, , & , , &

Minimum Spanning Tree

63

runs on graphsrunning time: without heaps, which is optimal for___________ graphsrunning time: using binary heaps, which is goodfor graphs

Minimum Spanning Tree

64

algorithmcontinually select the edges in order of ______________weightaccept the edge if it does not cause a withalready accepted edges

Page 17: graph theory Chapter 9 Graph Algorithms - William & …tadavis/cs303/ch09sm.pdf · Chapter 9 Graph Algorithms Introduction 2 graph theory ... 9 in a directed graph ... A. Church and

Minimum Spanning Tree

65

algorithm: example

Minimum Spanning Tree

66

algorithm: example 2

Minimum Spanning Tree

67

algorithm: example 2 (cont.)

Minimum Spanning Tree

68

time complexity: with proper choice and use ofdata structuresin the worst case, , so the worst-case timecomplexity is

Page 18: graph theory Chapter 9 Graph Algorithms - William & …tadavis/cs303/ch09sm.pdf · Chapter 9 Graph Algorithms Introduction 2 graph theory ... 9 in a directed graph ... A. Church and

NP-Complete Problems

69

What problems can we solve algorithmically? which problemsare easy? which problems are hard?

Eulerian circuit: Given a vertex s, start at s and find a cyclethat visits every exactly once

easy: solvable in using depth-first search

Hamiltonian circuit: Given a vertex s, start at s and find acycle that visits each remaining exactly once

really, really hard!

NP-Complete Problems

70

halting problemin 1936, A. Church and A. Turing independently proved thenon-solvability of the halting problem:

is there an algorithm terminates(p,x) that takes anarbitrary program p and input x and returns True if pterminates when given input x and False otherwise?

difficult: try to run it on _____________

NP-Complete Problems

71

halting problemsuppose we had such an algorithm terminates(p,x)create a new program:

program evil (z) {1: if terminates(z,z) goto 1

}

program evil() terminates if and only if the program zdoes not terminate when given its own code as input

no such algorithm can exist

NP-Complete Problems

72

decision problemhas a yes or no answerundecidable if it is to construct a singlealgorithm that will solve all instances of the problemthe halting problem is _______________

Page 19: graph theory Chapter 9 Graph Algorithms - William & …tadavis/cs303/ch09sm.pdf · Chapter 9 Graph Algorithms Introduction 2 graph theory ... 9 in a directed graph ... A. Church and

NP-Complete Problems

73

the class Pset of problems for which there exists a _______________time algorithm for their solutionthe runtime is bounded by a polynomial function of the_________ of the problem

the class NPset of decision problems for which the certification of acandidate ______________ as being correct can beperformed in polynomial timenon-deterministic polynomial time

NP-Complete Problems

74

the class NPfor problems in NP, certifying a solution may not be difficult,but a solution may be very difficultexample: Hamiltonian circuit

given a graph G, is there a simple cycle in G thatincludes every ?given a candidate solution, we can check whether it is asimple cycle in time , simply by thepathhowever, finding a Hamiltonian circuit is hard!

NP-Complete Problems

75

reductionsproblem A to problem B if the solvabilityof B implies the solvability of A

if A is reducible to B, then B is at least as hard to solveas A

in the context of algorithms, reducibility means analgorithm that solves B can be into analgorithm to solve A

example: if we can sort a set of numbers, we can findthe median, so finding the median reduces to sorting

NP-Complete Problems

76

reductionsproblem A can be reduced to B if we cansolve problem A using an algorithm for problem B suchthat the cost of solving A is

cost of solving B + a polynomial function of the problem size

example: once we have sorted an array ofnumbers, we can find the median in timeby computing and accessing

Page 20: graph theory Chapter 9 Graph Algorithms - William & …tadavis/cs303/ch09sm.pdf · Chapter 9 Graph Algorithms Introduction 2 graph theory ... 9 in a directed graph ... A. Church and

NP-Complete Problems

77

reductionsdecision version of traveling salesperson problem (TSP):

given a complete weighted graph and an integer ,does there exist a simple cycle that allvertices (tour) with total weight ?

clearly, this is in NPHamiltonian circuit: given a graph , find asimple cycle that visits all the vertices

construct a new graph with the same vertices asbut which is ; if an edge in is in ,give it weight 1; otherwise, give it weight 2

construction requires workapply to see if there exists a tour with totalweight

NP-Complete Problems

78

reductions

NP-Complete Problems

79

NP-completea problem A is NP-complete if it is in NP and all otherproblems in NP can be to A in polynomialtime

Boolean satisfiablity (SAT): given a set of N booleanvariables and M logical statements built from thevariables using and and not, can you choose values forthe variables so that all the statements are ?

SAT is NP-complete

NP-Complete Problems

80

NP-completeif we restrict attention to sets of boolean statementsinvolving variables, the problem is known as 3-SAT

3-SAT is NP-completeso, if you can solve 3-SAT in polynomial time, you cansolve problems in NP in polynomial timemeanwhile, 2-SAT is solvable in time!

Page 21: graph theory Chapter 9 Graph Algorithms - William & …tadavis/cs303/ch09sm.pdf · Chapter 9 Graph Algorithms Introduction 2 graph theory ... 9 in a directed graph ... A. Church and

NP-Complete Problems

81

NP-complete problemstraveling salespersonbin packingknapsackgraph coloringlongest-path

NP-Complete Problems

82

NP-hard problemsa problem A is NP-hard if there exists a polynomial-timereduction from an NP-complete problem to Aan NP-hard problem is at as hard as an NP-complete problem

versions of NP-complete problems aretypically NP-hard

optimization version of TSP: given a weighted graph,find a cost Hamiltonian circuitif we can solve TSP, we can solve Hamiltonian circuit

Bin Packing

83

Bin Packing

84

Page 22: graph theory Chapter 9 Graph Algorithms - William & …tadavis/cs303/ch09sm.pdf · Chapter 9 Graph Algorithms Introduction 2 graph theory ... 9 in a directed graph ... A. Church and

Bin Packing

85

Bin Packing

86

Bin Packing

87

Bin Packing

88

: 0.2, 0.5: 0.4: 0.7, 0.1: 0.3: 0.8

Page 23: graph theory Chapter 9 Graph Algorithms - William & …tadavis/cs303/ch09sm.pdf · Chapter 9 Graph Algorithms Introduction 2 graph theory ... 9 in a directed graph ... A. Church and

Bin Packing

89

Bin Packing

90

Bin Packing

91

Bin Packing

92

: 0.2, 0.5, 0.1: 0.4, 0.3: 0.7: 0.8

Page 24: graph theory Chapter 9 Graph Algorithms - William & …tadavis/cs303/ch09sm.pdf · Chapter 9 Graph Algorithms Introduction 2 graph theory ... 9 in a directed graph ... A. Church and

Bin Packing

93

Bin Packing

94

: 0.2, 0.5, 0.1: 0.4: 0.7, 0.3: 0.8

Bin Packing

95

: 0.8, 0.2: 0.7, 0.3: 0.5, 0.4, 0.1

Bin Packing

96