Java Foundations

Chapter 15
Graphs
Modified
Part A - Terminology and Traversals
Chapter Scope
• Directed and undirected graphs
• Weighted graphs (networks)
• Common graph algorithms
Java Software Structures, 4th Edition, Lewis/Chase
15 - 2
Terminology and Traversals
Java Software Structures, 4th Edition, Lewis/Chase
15 - 3
Graphs
• Like trees, graphs are made up of nodes and the
connections between those nodes.
• In graph terminology, we refer to the nodes as
vertices and refer to the connections among them as
edges
• Vertices are typically referenced by a name or label
• Edges are referenced by a pairing of the vertices (A,
B) that they connect
• An undirected graph is a graph where the pairings
representing the edges are unordered (order doesn’t
matter)
Java Software Structures, 4th Edition, Lewis/Chase
15 - 4
Undirected Graphs
• An undirected graph:
Java Software Structures, 4th Edition, Lewis/Chase
15 - 5
Undirected Graphs
• An edge in an undirected graph can be traversed
in either direction
• Two vertices are said to be adjacent if there is an
edge connecting them
• Adjacent vertices are sometimes referred to as
neighbors
• An edge of a graph that connects a vertex to
itself is called a self-loop or sling
• An undirected graph is considered complete if it
has the maximum possible number of edges
connecting vertices. How many edges?
Java Software Structures, 4th Edition, Lewis/Chase
15 - 6
Undirected Graphs
• A path is a sequence of edges that connects two
vertices in a graph
• The length of a path in is the number of edges in
the path (or the number of vertices minus 1)
• An undirected graph is considered connected if
for any two vertices in the graph there is a path
between them
Java Software Structures, 4th Edition, Lewis/Chase
15 - 7
Undirected Graphs
• An example of an undirected graph that is not
connected:
Java Software Structures, 4th Edition, Lewis/Chase
15 - 8
Cycles
• A cycle is a path in which the first and last
vertices are the same and none of the edges are
repeated
• A graph that has no cycles is called acyclic
Java Software Structures, 4th Edition, Lewis/Chase
15 - 9
Directed Graphs
• A directed graph, sometimes referred to as a
digraph, is a graph where the edges are ordered
pairs of vertices (edges have arrows)
• This means that the edges (A, B) and (B, A) are
separate, directional edges in a directed graph
Java Software Structures, 4th Edition, Lewis/Chase
15 - 10
Directed Graphs
• A directed graph with
– vertices A, B, C, D
– edges (A, B), (A, D), (B, C), (B, D) and (D, C)
Java Software Structures, 4th Edition, Lewis/Chase
15 - 11
Directed Graphs
• Previous definitions change slightly for directed
graphs
– a path in a direct graph is a sequence of directed
edges that connects two vertices in a graph
– a directed graph is connected if for any two vertices
in the graph there is a path between them
• if a directed graph has no cycles, it is possible to
arrange the vertices in a sequence such that
vertex A precedes vertex B in the sequence, if an
edge exists from A to B
Java Software Structures, 4th Edition, Lewis/Chase
15 - 12
Directed Graphs
• A connected directed graph and an unconnected
directed graph:
Java Software Structures, 4th Edition, Lewis/Chase
15 - 13
A tree as a graph
• A tree is an undirected acyclic graph.
• A rooted tree has one vertex
designated as the root.
• A directed tree is a directed graph with
edges directed away from the root, and
which would be a tree if the directions
on the edges were ignored
15 - 14
Weighted Graphs
• A weighted graph, sometimes called a network,
is a graph with weights (or costs) associated with
each edge
• The weight of a path in a weighted graph is the
sum of the weights of the edges in the path
• Weighted graphs may be either undirected or
directed
• For weighted graphs, we represent each edge
with a triple including the starting vertex, ending
vertex, and the weight (Boston, New York, 120)
Java Software Structures, 4th Edition, Lewis/Chase
15 - 15
Weighted Graphs
• We could use an undirected network to
represent flights between cities
• The weights are the cost of flight
Java Software Structures, 4th Edition, Lewis/Chase
15 - 16
Weighted Graphs
• A directed version of the graph could show
different costs depending on the direction
Java Software Structures, 4th Edition, Lewis/Chase
15 - 17
Common Graph Algorithms
• There are a number of a common algorithms that
may apply to undirected, directed, and/or
weighted graphs
• These include
–
–
–
–
various traversal algorithms
algorithms for finding the shortest path length (edge count)
algorithms for finding the least costly path in a network
algorithms for answering simple questions (such as
connectivity)
Java Software Structures, 4th Edition, Lewis/Chase
15 - 18
Graph Traversals
• There are two main types of graph traversal
algorithms
– breadth-first: behaves much like a level-order traversal of a
tree
– depth-first: behaves much like the preorder traversal of a tree
• One difference from trees: there is no root node
present in a graph
• Graph traversals may start at any vertex in the
graph
Java Software Structures, 4th Edition, Lewis/Chase
15 - 19
Graph Traversals
Java Software Structures, 4th Edition, Lewis/Chase
15 - 20
BFT starting from vertex v
create a queue Q
visit v, mark v as visited and put v into Q
while Q is non-empty
remove the head element u of Q
visit, mark & enqueue all (unvisited)
neighbors of u
Java Software Structures, 4th Edition, Lewis/Chase
15 - 21
DFT starting from vertex v
create a stack S
visit, mark v as visited and push v onto S
while S is non-empty
peek at the top u of S
if u has an (unvisited) neighbor w,
visit w, mark w and push it onto S
else pop S
Java Software Structures, 4th Edition, Lewis/Chase
15 - 22
Chapter 15
Graphs
Modified
Part B – Data Structures for Graphs
Basic Data Structures for Graphs
• Adjacency lists
• Adjacency matrices
Java Software Structures, 4th Edition, Lewis/Chase
15 - 24
Java Software Structures, 4th Edition, Lewis/Chase
15 - 25
For weighted graph - network
Can add weight field to nodes in adjacency lists.
15 - 26
Chapter 15
Graphs
Modified
Part C – Graph Algorithms 1
Basic Graph Algorithms
• Un-weighted graphs/digraphs
–Connectivity
–Cycle detection
–Shortest path
–Spanning tree
–Topological sort
Java Software Structures, 4th Edition, Lewis/Chase
15 - 28
Basic Graph Algorithms
• Weighted graphs/digraphs
–Minimum spanning tree
–Cheapest path
–Traveling salesman problem
Java Software Structures, 4th Edition, Lewis/Chase
15 - 29
Dynamic graphs
• Adding and removing vertices
• Adding and removing edges
Java Software Structures, 4th Edition, Lewis/Chase
15 - 30
Java Software Structures, 4th Edition, Lewis/Chase
15 - 31
Connectivity
• For a graph (undirected) how can we determine
if it is connected?
• Answer: Do a depth first traversal from any
vertex and see if all of the vertices are reached.
• For a digraph, how do can we determine if it is
connected?
• Answer: Do a depth first traversal from every
vertex and see if all other vertices are reached.
Java Software Structures, 4th Edition, Lewis/Chase
15 - 32
Java Software Structures, 4th Edition, Lewis/Chase
15 - 33
Cycle detection
• For a graph, how can we detect if it contains any
cycles?
• Do a depth first traversal and look for “back
edges”, edges from current node to a node
already visited.
• For a digraph –
• Do the same, for every vertex as starting vertex.
Java Software Structures, 4th Edition, Lewis/Chase
15 - 34
Finding shortest path (un-weighted)
• For a graph, how do we find the shortest path
(fewest edges) from vertex A to vertex B?
• Do a breadth first traversal starting at A and
going until B is reached, recording the path
length and predecessor at each node visited.
• Need more info stored at each node visited.
• For a digraph –
• Same
Java Software Structures, 4th Edition, Lewis/Chase
15 - 35
Shortest path form A to B
create a queue Q
visit A, mark A as visited, set path length=0, set
pred=null, and put A into Q
repeat until B is visited
remove the head element u of Q
for each w: (unvisited) neighbors of u
visit w, mark w, set path length of w, set
pred of w=u, and enqueue w
Follow the preds backwards from B to A
Java Software Structures, 4th Edition, Lewis/Chase
15 - 36
Java Software Structures, 4th Edition, Lewis/Chase
15 - 37
Chapter 15
Graphs
Modified
Part D – Graph Algorithms 2
Basic Graph Algorithms
• Un-weighted graphs/digraphs
–Spanning tree
–Topological sort
Java Software Structures, 4th Edition, Lewis/Chase
15 - 39
Java Software Structures, 4th Edition, Lewis/Chase
15 - 40
Constructing a spanning tree
• Assume an undirected graph
• A spanning tree is a subgraph of the original
graph which includes all of the vertices but only
some of the edges.
• If the graph has N vertices, the spanning tree will
have N-1 edges.
• It will have no cycles; it is a tree
• It uses the minimum number of edges to connect
all of the vertices together -- Unique?
• Lots of practical applications
• If a network contains any cycles, an edge can be
removed and the graph is still connected.
Java Software Structures, 4th Edition, Lewis/Chase
15 - 41
Finding a spanning tree
• Do a depth first traversal and
record all edges traversed to get to
a “new” edge.
Java Software Structures, 4th Edition, Lewis/Chase
15 - 42
Find Spanning tree –
DFT starting from vertex v
create a stack S
Initialize spanning tree edge list to empty
mark v as visited and push v onto S
while S is non-empty
peek at the top u of S
if u has an (unvisited) neighbor w,
add edge from u to w to spanning tree,
mark w and push it onto S
else pop S
Java Software Structures, 4th Edition, Lewis/Chase
15 - 43
Java Software Structures, 4th Edition, Lewis/Chase
15 - 44
Topological sort
A diagraph is acyclic if it has no cycles. Such a graph is
often referred to as a directed acyclic graph, or DAG,
for short. DAGs are used in many applications to
indicate precedence among events.
A directed graph G is acyclic if and only if a depth-first
search of G yields no back edges.
Topological sort of a DAG: a linear ordering (sequence)
of vertices such that if (u, v) is an edge of the DAG,
then u appears somewhere before v in that ordering.
Java Software Structures, 4th Edition, Lewis/Chase
15 - 45
Topological sort
• Do a DFT of the DAG and put vertices onto the
front of a linked list as they are finished.
• https://www.cs.usfca.edu/~galles/visualization/T
opoSortDFS.html
Java Software Structures, 4th Edition, Lewis/Chase
15 - 46
Alternate algorithm for top. sort of DAG
1. Compute the indegrees of all vertices
2. Find a vertex U with indegree 0 and append it to
the ordering
3. If there is no such vertex then there is a cycle
and the vertices cannot be ordered. Stop.
4. Remove U and all its edges (U,V) from the
graph.
5. Update the indegrees of the remaining vertices.
6. Repeat steps 2 through 4 while there are
vertices to be processed.
Java Software Structures, 4th Edition, Lewis/Chase
15 - 47
Chapter 15
Graphs
Modified
Part E – Graph Algorithms 3
Basic Graph Algorithms
• Weighted graphs/digraphs
–Minimum spanning tree
–Cheapest path
–Traveling salesman problem
Java Software Structures, 4th Edition, Lewis/Chase
15 - 49
Java Software Structures, 4th Edition, Lewis/Chase
15 - 50
MST for weighted graph
• We can also assign a weight to each edge, which
is a number representing how unfavorable it is,
and use this to assign a weight to a spanning tree
by computing the sum of the weights of the
edges in that spanning tree. A minimum
spanning tree (MST) or minimum weight
spanning tree is a spanning tree with weight less
than or equal to the weight of every other
spanning tree.
• We will assume positive weights only.
Java Software Structures, 4th Edition, Lewis/Chase
15 - 51
Greedy algorithms
• A greedy algorithm is an algorithm that follows
the problem solving heuristic of making the
locally optimal choice at each stage with the
hope of finding a global optimum.
Java Software Structures, 4th Edition, Lewis/Chase
15 - 52
Prim’s algorithm
1. Initialize a tree with a single vertex, chosen
arbitrarily from the graph.
2. Grow the tree by one edge: Of the edges that
connect the tree to vertices not yet in the tree,
find the minimum-weight edge, and transfer it
to the tree.
3. Repeat step 2 (until all vertices are in the tree).
https://www.cs.usfca.edu/~galles/visualization/Algorithms.html
Java Software Structures, 4th Edition, Lewis/Chase
15 - 53
Kruskal's algorithm
• create a forest F (a set of trees), where each vertex in
the graph is a separate tree
• create a set S containing all the edges in the graph
• while S is nonempty and F is not yet spanning
– remove an edge with minimum weight from S
– if that edge connects two different trees, then add it to
the forest, combining two trees into a single tree
• At the termination of the algorithm, the forest forms
a minimum spanning forest of the graph. If the graph
is connected, the forest has a single component and
forms a minimum spanning tree.
Java Software Structures, 4th Edition, Lewis/Chase
15 - 54
Shortest (cheapest) path in a weighted graph
• Dijkstra’s shortest path algorithm computes the
shortest paths to all vertices from a starting vertex.
• It is a greedy algorithm
• It works by growing a tree, adding the “closest” node
to the current tree to get the next version of the tree.
• The node added is the one that is adjacent to a node
already in the tree and closest to the start node using
only edges already included in the tree and one
frontier edge connecting it that tree.
• After adding a node, the distances of the nodes already
in the tree must be updated because the new node
may provide a shorter path to some of those nodes.
Java Software Structures, 4th Edition, Lewis/Chase
15 - 55
Let the node at which we are starting be called the initial node. Let
the distance of node Y be its distance from the initial node.
Dijkstra's algorithm will assign some initial distance values and will
try to improve them step by step.
1. Assign to every node a tentative distance value: set it to zero for our
initial node and to infinity for all other nodes.
2. Mark all nodes unvisited. Set the initial node as current. Create a set of
the unvisited nodes called the unvisited set consisting of all the nodes.
3. For the current node, consider all of its unvisited neighbors and
calculate their tentative distances. Compare the newly calculated
tentative distance to the current assigned value and assign the smaller
one.
For example, if the current node A is marked with a distance of 6, and
the edge connecting it with a neighbor B has length 2, then the
distance to B (through A) will be 6 + 2 = 8. If B was previously marked
with a distance greater than 8 then change it to 8. Otherwise, keep the
current value.
Java Software Structures, 4th Edition, Lewis/Chase
15 - 56
4. When we are done considering all of the neighbors of the current
node, mark the current node as visited and remove it from the
unvisited set. A visited node will never be checked again.
5. If all nodes have been marked visited or if the smallest tentative
distance among the nodes in the unvisited set is infinity (occurs
when there is no connection between the initial node and
remaining unvisited nodes), then stop. The algorithm has finished.
6. Select the unvisited node with the smallest tentative distance, and
set it as the new "current node" then go back to step 3.
Java Software Structures, 4th Edition, Lewis/Chase
15 - 57
First 5 steps in computing the shortest paths from A.
CN5E by Tanenbaum & Wetherall, © Pearson Education-Prentice Hall and D. Wetherall, 2011
Dijkstra's algorithm picks the unvisited vertex with the
lowest-distance.
It then calculates the distance through it to each unvisited
neighbor, and updates the neighbor's distance if smaller.
Marked visited (set to red) when done with neighbors.
Java Software Structures, 4th Edition, Lewis/Chase
15 - 59
Animated demos
https://www.cs.usfca.edu/~galles/visualization/Algorithms.html
Java Software Structures, 4th Edition, Lewis/Chase
15 - 60
Traveling salesman problem
Given a list of cities and the distances between
each pair of cities, what is the shortest possible
route that visits each city exactly once and
returns to the origin city?
Java Software Structures, 4th Edition, Lewis/Chase
15 - 61
Brute Force Solutions of TSP
The most direct solution would be to try all
permutations (ordered combinations) and see
which one is cheapest (using brute force search).
The running time for this is O(N!) where N is the
number of cities, so this solution becomes
impractical somewhere around 20 cities.
Java Software Structures, 4th Edition, Lewis/Chase
15 - 62
An exact solution for 15,112 German towns was
found in 2001 using the cutting-plane method
proposed by George Dantzig, Ray Fulkerson, and
Selmer M. Johnson in 1954, based on linear
programming.
The computations were performed on a network
of 110 processors located at Rice University and
Princeton University. The total computation time
was equivalent to 22.6 years on a single 500 MHz
Alpha processor.
Java Software Structures, 4th Edition, Lewis/Chase
15 - 63
In March 2005, the travelling salesman problem
of visiting all 33,810 points in a circuit board was
solved using Concorde TSP Solver: a tour of
length 66,048,945 units was found and it was
proven that no shorter tour exists. The
computation took approximately 15.7 CPU-years.
In April 2006 an instance with 85,900 points was
solved using Concorde TSP Solver, taking over 136
CPU-years.
Java Software Structures, 4th Edition, Lewis/Chase
15 - 64
TSP is an NP-hard problem
Comp 314/Comp 332
Java Software Structures, 4th Edition, Lewis/Chase
15 - 65
Chapter 15
Graphs
Modified
Part F – Graph Algorithms 4
Dynamic graphs
• Adding and removing vertices
• Adding and removing edges
Java Software Structures, 4th Edition, Lewis/Chase
15 - 67
Java Software Structures, 4th Edition, Lewis/Chase
15 - 68
Dynamic structures of graphs
• So far we have worked with static data structures
for graphs.
• We didn’t add vertices or edges, and we didn’t
delete vertices or edges.
• Now let’s consider how to handle those cases
Java Software Structures, 4th Edition, Lewis/Chase
15 - 69
With adjacency matrices
• Adding a vertex means adding a row and column to
a matrix
• Adding an edge (assuming the vertices are already
there) means filling in an entry (or two) in the matrix
• Deleting a vertex means removing a row and a
column from the matrix, which deletes the edges
incident with that vertex
• Deleting an edge means zeroing out an entry in the
matrix.
• Arrays are not the most flexible structures for adding
and deleting.
Java Software Structures, 4th Edition, Lewis/Chase
15 - 70
With adjacency lists
• Adding a vertex means adding a new list (with any
additional vertex info you are keeping).
• Adding an edge means adding node(s) to all list(s)
for vertices that edge is incident with.
• Deleting a vertex means removing a list; this will
delete the edges from that vertex, and other lists
may have to be updated as well
• Deleting an edge means removing the node(s) for it
in the appropriate list(s).
• This structure is more flexible, especially if you use
an ArrayList, or any list for the vertex info and links.
Java Software Structures, 4th Edition, Lewis/Chase
15 - 71