96
Chapter 9 Graph Algorithms

Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Chapter 9

Graph Algorithms

Page 2: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Introduction

2

โˆ’graph theory

โˆ’useful in practice

โˆ’ represent many real-life problems

โˆ’can be slow if not careful with data structures

Page 3: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Definitions

3

โˆ’an undirected graph ๐บ = (๐‘‰, ๐ธ) is a finite set ๐‘‰ of vertices

together with a set ๐ธ of edges

โˆ’an edge is a pair (๐‘ฃ, ๐‘ค), where ๐‘ฃ and ๐‘ค are vertices

โˆ’ this definition allows

โˆ’self-loops, 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

Page 4: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Definitions

4

โˆ’ two vertices are adjacent if there is an edge 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 incident to it

โˆ’self-loops add only 1 to the degree

โˆ’a subgraph of a graph ๐บ is a subset of ๐บ's edges together

with the incident vertices

Page 5: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Definitions

5

โˆ’a path in a graph is a sequence of vertices connected by

edges

โˆ’a simple path is a path with no repeated vertices, except

possibly the first and last

โˆ’a cycle is a path of at least one edge whose first and last

vertices are the same

โˆ’a simple cycle is a cycle with no repeated edges of

vertices other than the first and last

โˆ’ the length of a path is the number of edges in the path

Page 6: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Definitions

6

โˆ’a graph is connected if every vertex is connected to every

other vertex by a path through the graph

โˆ’a connected component ๐บโ€ฒ of a graph ๐บ is a maximal

connected subgraph of ๐บ: if ๐บโ€ฒ is a subset of ๐น and ๐น is a

connected subgraph of ๐บ, then ๐น = ๐บโ€ฒ

โˆ’a graph that is not connected consists of a set of connected

components

โˆ’a graph without cycles is called acyclic

Page 7: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Definitions

7

โˆ’a tree is a connected, acyclic undirected graph

โˆ’a forest is a disjoint set of trees

โˆ’a spanning tree of a connected graph is a subgraph that is a

tree and also contains all of the graph's vertices

โˆ’a spanning forest of a graph is the union of spanning trees

of its connected components

Page 8: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Definitions

8

โˆ’ if ๐‘‰ is the number of vertices and ๐ธ is the number of

edges, then, in a graph without self-loops and parallel

edges, there are ๐‘‰ ( ๐‘‰ โˆ’ 1)/2 possible edges

โˆ’a graph is complete if there is an edge between every pair

of vertices

โˆ’ the density of a graph refers to the proportion of possible

pairs of vertices that are connected

โˆ’a sparse graph is one for which ๐ธ โ‰ช ๐‘‰ ( ๐‘‰ โˆ’ 1)/2

โˆ’a dense graph is a graph that is not sparse

โˆ’a bipartite graph is one whose vertices can be divided into

two sets so that every vertex in one set is connected to at

least one vertex in the other set

Page 9: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Definitions

9

โˆ’ in a directed graph or digraph the pairs (๐‘ฃ, ๐‘ค) indicating edges are ordered: the edge (๐‘ฃ, ๐‘ค) goes from ๐‘ฃ (the tail) to ๐‘ค (the head)

โˆ’since edges have a direction, we use the notation ๐‘ฃ โ†’ ๐‘ค to denote an edge from ๐‘ฃ to ๐‘ค

โˆ’edges in digraphs are frequently called arcs

โˆ’ the indegree of a vertex ๐‘ค is the number of arcs ๐‘ฃ โ†’ ๐‘ค (i.e., the number of arcs coming into ๐‘ค), while the outdegree of ๐‘ค is the number of arcs ๐‘ค โ†’ ๐‘ฃ (i.e., the number of arcs exiting ๐‘ค)

โˆ’we will call ๐‘ค a source if its indegree is 0

โˆ’an aborescence is a directed graph with a distinguished vertex ๐‘ข (the root) such that for every other vertex ๐‘ฃ there is a unique directed path from ๐‘ข to ๐‘ฃ

Page 10: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Definitions

10

โˆ’ in a directed graph, two vertices ๐‘ฃ and ๐‘ค are strongly

connected if there is a directed path from ๐‘ฃ to ๐‘ค and a

directed path from ๐‘ค to ๐‘ฃ

โˆ’a digraph is strongly connected if all its vertices are strongly

connected

โˆ’ if a digraph is not strongly connected but the underlying

undirected graph is connected, then the digraph is called

weakly connected

โˆ’a weighted graph has weights or costs associated with each

edge

โˆ’weighted graphs can be directed or undirected

โˆ’a road map with mileage is the prototypical example

Page 11: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Example

11

โˆ’airport connections

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

Page 12: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Graph Representation

12

โˆ’ two concerns: memory and speed

โˆ’weโ€™ll consider directed graphs, though undirected graphs are

similar

โˆ’ the following graph has 7 vertices and 12 edges

Page 13: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Graph Representation

13

โˆ’adjacency matrix

โˆ’2D matrix where an element is 1 if (๐‘ข, ๐‘ฃ) โˆˆ ๐ด and 0

otherwise

Page 14: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Graph Representation

14

โˆ’adjacency matrix

โˆ’alternatively, could use costs โˆž or โˆ’โˆž for non-edges

โˆ’not efficient if the graph is sparse (number of edges small)

โˆ’matrix ๐‘‚( ๐‘‰ 2)

โˆ’e.g., street map with 3,000 streets results in intersection

matrix with 9,000,000 elements

โˆ’adjacency list

โˆ’standard way to represent graphs

โˆ’undirected graph edges appear twice in list

โˆ’more efficient if the graph is sparse (number of edges

small)

โˆ’matrix ๐‘‚( ๐ธ + ๐‘‰ )

Page 15: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Graph Representation

15

โˆ’adjacency list

โˆ’ for each vertex, keep a list of adjacent vertices

Page 16: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Graph Representation

16

โˆ’adjacency list alternative

โˆ’ for each vertex, keep a vector of adjacent vertices

Page 17: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Topological Sort

17

โˆ’a directed acyclic graph (DAG) is a digraph with no directed

cycles

โˆ’a DAG always has at least one vertex

โˆ’ topological sort

โˆ’an ordering of the vertices in a directed graph such that if

there is a path from v to w, then v appears before w in the

ordering

โˆ’not possible if graph has a cycle

Page 18: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Topological Sort

18

โˆ’example directed acyclic graph

Page 19: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Topological Sort

19

โˆ’ topological sort

โˆ’determine the indegree for every ๐‘ฃ โˆˆ ๐‘‰

โˆ’place all source vertices in a queue

โˆ’while there remains a ๐‘ฃ โˆˆ ๐‘‰

โˆ’find a source vertex

โˆ’append the source vertex to the topological sort

โˆ’delete the source and its adjacent arcs from ๐บ

โˆ’update the indegrees of the remaining vertices in ๐บ

โˆ’place any new source vertices in the queue

โˆ’when no vertices remain, we have our ordering, or, if we are

missing vertices from the output list, the graph has no

topological sort

Page 20: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Topological Sort

20

โˆ’since finding vertex with 0 indegree must look at all vertices,

and this is performed ๐‘‰ times, ๐‘‚( ๐‘‰ 2)

Page 21: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Topological Sort

21

โˆ’ instead, we can keep

all the vertices with

indegree 0 in a list and

choose from there

โˆ’๐‘‚( ๐ธ + ๐‘‰ )

Page 22: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Topological Sort

22

โˆ’use a table to keep track of the source vertices

Page 23: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Shortest-Path Algorithms

23

โˆ’shortest-path problems

โˆ’ input is a weighted graph with a cost on each edge

โˆ’weighted path length: ๐‘๐‘–,๐‘–+1๐‘โˆ’1๐‘–=1

โˆ’single-source shortest-path problem

โˆ’given as input a weighted graph, ๐บ = (๐‘‰, ๐ธ) and a

distinguished vertex ๐‘ , find the shortest weighted path from

๐‘  to every other vertex in ๐บ

Page 24: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Shortest-Path Algorithms

24

โˆ’example

โˆ’shortest weighted path from ๐‘ฃ1 to ๐‘ฃ6 has cost of 6

โˆ’no path from ๐‘ฃ6 to ๐‘ฃ1

Page 25: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Shortest-Path Algorithms

25

โˆ’negative edges can cause problems

โˆ’path from ๐‘ฃ5 to ๐‘ฃ4 has cost of 1, but a shorter path exists by

following the negative loop, which has cost -5

โˆ’shortest paths thus undefined

Page 26: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Shortest-Path Algorithms

26

โˆ’many examples where we want to find shortest paths

โˆ’ if vertices represent computers and edges connections, the

cost represents communication costs, delay costs, or

combination of costs

โˆ’ if vertices represent airports and edges costs to travel

between them, shortest path is cheapest route

โˆ’we find paths from one vertex to all others since no algorithm

exists that finds shortest path from one vertex to one other

faster

Page 27: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Shortest-Path Algorithms

27

โˆ’ four problems

โˆ’unweighted shortest-path

โˆ’weighted shortest-path with no negative edges

โˆ’weighted shortest-path with negative edges

โˆ’weighted shortest-path in acyclic graphs

Page 28: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Unweighted Shortest Paths

28

โˆ’unweighted shortest-path

โˆ’ find shortest paths from ๐‘  to all other vertices

โˆ’only concerned with number of edges in path

โˆ’we will not actually record the path elements

Page 29: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Unweighted Shortest Paths

29

โˆ’example

โˆ’start with ๐‘ฃ3

Page 30: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Unweighted Shortest Paths

30

โˆ’example

โˆ’mark 0 length to ๐‘ฃ3

Page 31: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Unweighted Shortest Paths

31

โˆ’example

โˆ’mark 1 length for ๐‘ฃ1 and ๐‘ฃ6

Page 32: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Unweighted Shortest Paths

32

โˆ’example

โˆ’mark 2 length for ๐‘ฃ2 and ๐‘ฃ4

Page 33: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Unweighted Shortest Paths

33

โˆ’example

โˆ’ final path assignments

Page 34: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Unweighted Shortest Paths

34

โˆ’searching an unweighted shortest-path uses a breadth-first

search

โˆ’processes vertices in layers, according to distance

โˆ’begins with initializing path lengths

โˆ’a vertex will be marked known when the shortest path to it

is found

Page 35: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Unweighted Shortest Paths

35

Page 36: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Unweighted Shortest Paths

36

โˆ’with this algorithm

โˆ’path can be printed

โˆ’ running time: ๐‘‚( ๐‘‰ 2)

โˆ’bad case

โˆ’can reduce time by keeping vertices that are unknown

separate from those known

โˆ’new running time: ๐‘‚( ๐ธ + ๐‘‰ )

Page 37: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Unweighted Shortest Paths

37

Page 38: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Unweighted Shortest Paths

38

Page 39: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Dijkstraโ€™s Algorithm

39

โˆ’weighted shortest-path โ€“ Dijkstraโ€™s algorithm

โˆ’more difficult, but ideas from unweighted algorithm can be used

โˆ’keep information as before for each vertex

โˆ’known

โˆ’set distance ๐‘‘๐‘ค = ๐‘‘๐‘ฃ + ๐‘๐‘ฃ,๐‘ค if ๐‘‘๐‘ค = โˆž using only known vertices

โˆ’๐‘๐‘ฃ the last vertex to cause a change to ๐‘‘๐‘ฃ

โˆ’greedy algorithm

โˆ’does what appears to be best thing at each stage

โˆ’e.g., counting money: count quarters first, then dimes, nickels, pennies

โˆ’gives change with least number of coins

Page 40: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Dijkstraโ€™s Algorithm

40

โˆ’pseudocode

โˆ’assumption: no negative weights

โˆ’origin ๐‘  is given

Page 41: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Dijkstraโ€™s Algorithm

41

โˆ’pseudocode (cont.)

Page 42: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Dijkstraโ€™s Algorithm

42

โˆ’example: start at ๐‘ฃ1

Page 43: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Dijkstraโ€™s Algorithm

43

โˆ’example

๐‘ฃ1 ๐‘ฃ4 ๐‘ฃ2

Page 44: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Dijkstraโ€™s Algorithm

44

โˆ’example

๐‘ฃ5, ๐‘ฃ3 ๐‘ฃ7 ๐‘ฃ6

Page 45: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Dijkstraโ€™s Algorithm

45

โˆ’example โ€“ stages shown on the graph

Page 46: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Dijkstraโ€™s Algorithm

46

โˆ’example โ€“ stages shown on the graph (cont.)

Page 47: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Dijkstraโ€™s Algorithm: Correctness

47

โˆ’correctness

Page 48: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Dijkstraโ€™s Algorithm

48

โˆ’complexity

โˆ’sequentially scanning vertices to find minimum ๐‘‘๐‘ฃ takes

๐‘‚( ๐‘‰ ), which results in ๐‘‚( ๐‘‰ 2) overall

โˆ’at most one update per edge, for a total of ๐‘‚ ๐ธ + ๐‘‰ 2 =๐‘‚( ๐‘‰ 2)

โˆ’ if graph is dense, with ๐ธ = ฯด ๐‘‰ 2 , algorithm is close to

optimal

โˆ’ if graph is sparse, with ๐ธ = ฯด( ๐‘‰ ), algorithm is too slow

โˆ’distances could be kept in a priority queue that reduces

running time to ๐‘‚ ๐ธ + ๐‘‰ lg ๐‘‰

Page 49: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Dijkstraโ€™s Algorithm

49

โˆ’ implementation

โˆ’ information for each vertex

Page 50: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Dijkstraโ€™s Algorithm

50

โˆ’ implementation (cont.)

โˆ’path can be printed recursively

Page 51: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Dijkstraโ€™s Algorithm

51

โˆ’ implementation (cont.)

Page 52: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Graphs with Negative Edges

52

โˆ’ try to apply Dijkstraโ€™s algorithm to graph with negative edges

Page 53: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Graphs with Negative Edges

53

โˆ’possible solution: add a delta value to all weights such that

none are negative

โˆ’calculate shortest path on new graph

โˆ’apply path to original graph

โˆ’does not work: longer paths become weightier

โˆ’combination of algorithms for weighted graphs and

unweighted graphs can work

โˆ’drastic increase in running time: ๐‘‚ ๐ธ โˆ™ ๐‘‰

Page 54: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

All-Pairs Shortest Paths

54

โˆ’given a weighted digraph, find the shortest paths between all

vertices in the graph

โˆ’one approach: apply Dijkstra's algorithm repeatedly

โˆ’ results in ๐‘‚( ๐‘‰ 3)

โˆ’another approach: apply Floyd-Warshall algorithm

โˆ’uses dynamic programming

โˆ’also results in ๐‘‚( ๐‘‰ 3)

Page 55: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Minimum Spanning Tree

55

โˆ’assumptions

โˆ’graph is connected

โˆ’edge weights are not necessarily Euclidean distances

โˆ’edge weights need not be all the same

โˆ’edge weights may be zero or negative

โˆ’minimum spanning tree (MST)

โˆ’also called minimum-weight spanning tree of a weighted

graph

โˆ’spanning tree whose weight (the sum of the weights of the

edges in the tree) is the smallest among all spanning trees

Page 56: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Minimum Spanning Tree

56

example

Page 57: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Minimum Spanning Tree

57

โˆ’ two algorithms to find the minimum spanning tree

โˆ’Primโ€™s Algorithm

โˆ’R. C. Prim, Shortest Connection Networks and Some

Generalizations, Bell System Technical Journal (1957)

โˆ’Kruskalโ€™s Algorithm

โˆ’J. B. Kruskal, Jr., On the Shortest Spanning Subtree of a

Graph and the Traveling Salesman Problem, Proc. of the

American Mathematical Society (1956)

Page 58: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Minimum Spanning Tree

58

โˆ’Primโ€™s algorithm

โˆ’grows tree in successive stages

Page 59: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Minimum Spanning Tree

59

โˆ’Primโ€™s algorithm: example

Page 60: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Minimum Spanning Tree

60

โˆ’Primโ€™s algorithm: example 2

Page 61: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Minimum Spanning Tree

61

โˆ’Primโ€™s algorithm: example 2 (cont.)

Page 62: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Minimum Spanning Tree

62

โˆ’Primโ€™s algorithm: example 2 (cont.)

โˆ’๐‘ฃ1, ๐‘ฃ4, ๐‘ฃ2 & ๐‘ฃ3, ๐‘ฃ7, ๐‘ฃ6 & ๐‘ฃ5

Page 63: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Minimum Spanning Tree

63

โˆ’Primโ€™s algorithm

โˆ’ runs on undirected graphs

โˆ’ running time: ๐‘‚( ๐‘‰ 2) without heaps, which is optimal for

dense graphs

โˆ’ running time: ๐‘‚ ๐ธ lg ๐‘‰ using binary heaps, which is good

for sparse graphs

Page 64: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Minimum Spanning Tree

64

โˆ’Kruskalโ€™s algorithm

โˆ’continually select the edges in order of smallest weight

โˆ’accept the edge if it does not cause a cycle with already

accepted edges

Page 65: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Minimum Spanning Tree

65

โˆ’Kruskalโ€™s algorithm: example

Page 66: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Minimum Spanning Tree

66

โˆ’Kruskalโ€™s algorithm: example 2

Page 67: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Minimum Spanning Tree

67

โˆ’Kruskalโ€™s algorithm: example 2 (cont.)

Page 68: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Minimum Spanning Tree

68

โˆ’Kruskalโ€™s algorithm

โˆ’ time complexity: ๐‘‚ ๐ธ lg ๐ธ with proper choice and use of

data structures

โˆ’ in the worst case, ๐ธ = ฯด ๐‘‰ 2 , so the worst-case time

complexity is ๐‘‚ ๐ธ lg ๐‘‰

Page 69: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

NP-Complete Problems

69

โˆ’What problems can we solve algorithmically? which problems

are easy? which problems are hard?

โˆ’Eulerian circuit: Given a vertex s, start at s and find a cycle

that visits every edge exactly once

โˆ’easy: solvable in ๐‘‚ ๐ธ + ๐‘‰ using depth-first search

โˆ’Hamiltonian circuit: Given a vertex s, start at s and find a

cycle that visits each remaining vertex exactly once

โˆ’really, really hard!

Page 70: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

NP-Complete Problems

70

โˆ’halting problem

โˆ’ in 1936, A. Church and A. Turing independently proved the

non-solvability of the halting problem:

โˆ’ is there an algorithm terminates(p,x) that takes an

arbitrary program p and input x and returns True if p

terminates when given input x and False otherwise?

โˆ’difficult: try to run it on itself

Page 71: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

NP-Complete Problems

71

โˆ’halting problem

โˆ’suppose 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 z

does not terminate when given its own code as input

โˆ’no such algorithm can exist

Page 72: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

NP-Complete Problems

72

โˆ’decision problem

โˆ’has a yes or no answer

โˆ’undecidable if it is impossible to construct a single

algorithm that will solve all instances of the problem

โˆ’ the halting problem is undecidable

Page 73: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

NP-Complete Problems

73

โˆ’ the class P

โˆ’set of problems for which there exists a polynomial time

algorithm for their solution

โˆ’ the runtime is bounded by a polynomial function of the size

of the problem

โˆ’ the class NP

โˆ’set of decision problems for which the certification of a

candidate solution as being correct can be performed in

polynomial time

โˆ’non-deterministic polynomial time

Page 74: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

NP-Complete Problems

74

โˆ’ the class NP

โˆ’ for problems in NP, certifying a solution may not be difficult,

but finding a solution may be very difficult

โˆ’example: Hamiltonian circuit

โˆ’given a graph G, is there a simple cycle in G that

includes every vertex?

โˆ’given a candidate solution, we can check whether it is a

simple cycle in time โˆ ๐‘‰ , simply by traversing the path

โˆ’however, finding a Hamiltonian circuit is hard!

Page 75: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

NP-Complete Problems

75

โˆ’ reductions

โˆ’problem A reduces to problem B if the solvability of B

implies the solvability of A

โˆ’ if A is reducible to B, then B is at least as hard to solve

as A

โˆ’ in the context of algorithms, reducibility means an

algorithm that solves B can be converted into an

algorithm to solve A

โˆ’example: if we can sort a set of numbers, we can find

the median, so finding the median reduces to sorting

Page 76: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

NP-Complete Problems

76

โˆ’ reductions

โˆ’problem A can be polynomially reduced to B if we can

solve problem A using an algorithm for problem B such

that the cost of solving A is

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

โˆ’example: once we have sorted an array ๐‘Ž[] of ๐‘

numbers, we can find the median in constant time by

computing ๐‘ 2 and accessing ๐‘Ž ๐‘ 2

Page 77: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

NP-Complete Problems

77

โˆ’ reductions

โˆ’decision version of traveling salesperson problem (TSP):

โˆ’given a complete weighted graph and an integer ๐พ, does there exist a simple cycle that visits all vertices (tour) with total weight โ‰ค ๐พ?

โˆ’clearly, this is in NP

โˆ’Hamiltonian circuit: given a graph ๐บ = (๐‘‰, ๐ธ), find a simple cycle that visits all the vertices

โˆ’construct a new graph ๐บโ€ฒ with the same vertices as ๐บ but which is complete; if an edge in ๐บโ€ฒ is in ๐บ, give it weight 1; otherwise, give it weight 2

โˆ’construction requires ๐‘‚ ๐ธ + ๐‘‰ work

โˆ’apply TSP to see if there exists a tour with total weight ๐‘‰

Page 78: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

NP-Complete Problems

78

โˆ’ reductions

Page 79: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

NP-Complete Problems

79

โˆ’NP-complete

โˆ’a problem A is NP-complete if it is in NP and all other

problems in NP can be reduced to A in polynomial time

โˆ’Boolean satisfiablity (SAT): given a set of N boolean

variables and M logical statements built from the

variables using and and not, can you choose values for

the variables so that all the statements are true?

๐‘ฅ1 ๐ด๐‘๐ท ! ๐‘ฅ2 ๐ด๐‘๐ท ๐‘ฅ3 , ! ๐‘ฅ1 ๐ด๐‘๐ท ๐‘ฅ7 , ๐‘ฅ1 ๐ด๐‘๐ท ๐‘ฅ42 , โ€ฆ

โˆ’SAT is NP-complete

Page 80: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

NP-Complete Problems

80

โˆ’NP-complete

โˆ’ if we restrict attention to sets of boolean statements

involving 3 variables, the problem is known as 3-SAT

โˆ’3-SAT is NP-complete

โˆ’so, if you can solve 3-SAT in polynomial time, you can

solve all problems in NP in polynomial time

โˆ’meanwhile, 2-SAT is solvable in linear time!

Page 81: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

NP-Complete Problems

81

โˆ’NP-complete problems

โˆ’ traveling salesperson

โˆ’bin packing

โˆ’knapsack

โˆ’graph coloring

โˆ’ longest-path

Page 82: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

NP-Complete Problems

82

โˆ’NP-hard problems

โˆ’a problem A is NP-hard if there exists a polynomial-time

reduction from an NP-complete problem to A

โˆ’an NP-hard problem is at least as hard as an NP-

complete problem

โˆ’optimization versions of NP-complete problems are

typically NP-hard

โˆ’optimization version of TSP: given a weighted graph,

find a minimum cost Hamiltonian circuit

โˆ’ if we can solve TSP, we can solve Hamiltonian circuit

Page 83: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Bin Packing

83

Page 84: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Bin Packing

84

Page 85: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Bin Packing

85

Page 86: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Bin Packing

86

Page 87: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Bin Packing

87

Page 88: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Bin Packing

88

๐ต1: 0.2, 0.5

๐ต2: 0.4

๐ต3: 0.7, 0.1

๐ต4: 0.3

๐ต5: 0.8

Page 89: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Bin Packing

89

Page 90: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Bin Packing

90

Page 91: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Bin Packing

91

Page 92: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Bin Packing

92

๐ต1: 0.2, 0.5, 0.1

๐ต2: 0.4, 0.3

๐ต3: 0.7

๐ต4: 0.8

Page 93: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Bin Packing

93

Page 94: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Bin Packing

94

๐ต1: 0.2, 0.5, 0.1

๐ต2: 0.4

๐ต3: 0.7, 0.3

๐ต4: 0.8

Page 95: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Bin Packing

95

๐ต1: 0.8, 0.2

๐ต2: 0.7, 0.3

๐ต3: 0.5, 0.4, 0.1

Page 96: Chapter 9 Graph Algorithmstadavis/cs303/ch09.pdfย ยท Definitions 3 โˆ’an undirected graph = (๐‘‰, ) is a finite set ๐‘‰ of vertices together with a set of edges โˆ’an edge is a pair

Bin Packing

96