Upload
others
View
9
Download
0
Embed Size (px)
Citation preview
Chapter 9
Graph Algorithms
Introduction
2
โgraph theory
โuseful in practice
โ represent many real-life problems
โcan be slow if not careful with data structures
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
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
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
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
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
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
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 ๐ฃ
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
Example
11
โairport connections
http://allthingsgraphed.com/2014/08/09/us-airlines-graph/
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
Graph Representation
13
โadjacency matrix
โ2D matrix where an element is 1 if (๐ข, ๐ฃ) โ ๐ด and 0
otherwise
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 ๐( ๐ธ + ๐ )
Graph Representation
15
โadjacency list
โ for each vertex, keep a list of adjacent vertices
Graph Representation
16
โadjacency list alternative
โ for each vertex, keep a vector of adjacent vertices
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
Topological Sort
18
โexample directed acyclic graph
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
Topological Sort
20
โsince finding vertex with 0 indegree must look at all vertices,
and this is performed ๐ times, ๐( ๐ 2)
Topological Sort
21
โ instead, we can keep
all the vertices with
indegree 0 in a list and
choose from there
โ๐( ๐ธ + ๐ )
Topological Sort
22
โuse a table to keep track of the source vertices
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 ๐บ
Shortest-Path Algorithms
24
โexample
โshortest weighted path from ๐ฃ1 to ๐ฃ6 has cost of 6
โno path from ๐ฃ6 to ๐ฃ1
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
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
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
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
Unweighted Shortest Paths
29
โexample
โstart with ๐ฃ3
Unweighted Shortest Paths
30
โexample
โmark 0 length to ๐ฃ3
Unweighted Shortest Paths
31
โexample
โmark 1 length for ๐ฃ1 and ๐ฃ6
Unweighted Shortest Paths
32
โexample
โmark 2 length for ๐ฃ2 and ๐ฃ4
Unweighted Shortest Paths
33
โexample
โ final path assignments
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
Unweighted Shortest Paths
35
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: ๐( ๐ธ + ๐ )
Unweighted Shortest Paths
37
Unweighted Shortest Paths
38
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
Dijkstraโs Algorithm
40
โpseudocode
โassumption: no negative weights
โorigin ๐ is given
Dijkstraโs Algorithm
41
โpseudocode (cont.)
Dijkstraโs Algorithm
42
โexample: start at ๐ฃ1
Dijkstraโs Algorithm
43
โexample
๐ฃ1 ๐ฃ4 ๐ฃ2
Dijkstraโs Algorithm
44
โexample
๐ฃ5, ๐ฃ3 ๐ฃ7 ๐ฃ6
Dijkstraโs Algorithm
45
โexample โ stages shown on the graph
Dijkstraโs Algorithm
46
โexample โ stages shown on the graph (cont.)
Dijkstraโs Algorithm: Correctness
47
โcorrectness
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 ๐
Dijkstraโs Algorithm
49
โ implementation
โ information for each vertex
Dijkstraโs Algorithm
50
โ implementation (cont.)
โpath can be printed recursively
Dijkstraโs Algorithm
51
โ implementation (cont.)
Graphs with Negative Edges
52
โ try to apply Dijkstraโs algorithm to graph with negative edges
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: ๐ ๐ธ โ ๐
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)
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
Minimum Spanning Tree
56
example
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)
Minimum Spanning Tree
58
โPrimโs algorithm
โgrows tree in successive stages
Minimum Spanning Tree
59
โPrimโs algorithm: example
Minimum Spanning Tree
60
โPrimโs algorithm: example 2
Minimum Spanning Tree
61
โPrimโs algorithm: example 2 (cont.)
Minimum Spanning Tree
62
โPrimโs algorithm: example 2 (cont.)
โ๐ฃ1, ๐ฃ4, ๐ฃ2 & ๐ฃ3, ๐ฃ7, ๐ฃ6 & ๐ฃ5
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
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
Minimum Spanning Tree
65
โKruskalโs algorithm: example
Minimum Spanning Tree
66
โKruskalโs algorithm: example 2
Minimum Spanning Tree
67
โKruskalโs algorithm: example 2 (cont.)
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 ๐
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!
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
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
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
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
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!
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
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
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 ๐
NP-Complete Problems
78
โ reductions
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
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!
NP-Complete Problems
81
โNP-complete problems
โ traveling salesperson
โbin packing
โknapsack
โgraph coloring
โ longest-path
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
Bin Packing
83
Bin Packing
84
Bin Packing
85
Bin Packing
86
Bin Packing
87
Bin Packing
88
๐ต1: 0.2, 0.5
๐ต2: 0.4
๐ต3: 0.7, 0.1
๐ต4: 0.3
๐ต5: 0.8
Bin Packing
89
Bin Packing
90
Bin Packing
91
Bin Packing
92
๐ต1: 0.2, 0.5, 0.1
๐ต2: 0.4, 0.3
๐ต3: 0.7
๐ต4: 0.8
Bin Packing
93
Bin Packing
94
๐ต1: 0.2, 0.5, 0.1
๐ต2: 0.4
๐ต3: 0.7, 0.3
๐ต4: 0.8
Bin Packing
95
๐ต1: 0.8, 0.2
๐ต2: 0.7, 0.3
๐ต3: 0.5, 0.4, 0.1
Bin Packing
96