chapter 15 graphs modified part a - terminology and traversals

71
Chapter 15 Graphs Modified A - Terminology and Travers

Upload: jasmine-tapp

Post on 15-Dec-2015

222 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Chapter 15

Graphs

Modified

Part A - Terminology and Traversals

Page 2: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

Chapter Scope• Directed and undirected graphs• Weighted graphs (networks)• Common graph algorithms

15 - 2

Page 3: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

Terminology and Traversals

15 - 3

Page 4: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

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)

15 - 4

Page 5: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

Undirected Graphs• An undirected graph:

15 - 5

Page 6: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

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?

15 - 6

Page 7: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

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

15 - 7

Page 8: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

Undirected Graphs• An example of an undirected graph that is not

connected:

15 - 8

Page 9: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

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

15 - 9

Page 10: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

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

15 - 10

Page 11: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

Directed Graphs• A directed graph with

– vertices A, B, C, D– edges (A, B), (A, D), (B, C), (B, D) and (D, C)

15 - 11

Page 12: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

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

15 - 12

Page 13: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

Directed Graphs• A connected directed graph and an unconnected

directed graph:

15 - 13

Page 14: Chapter 15 Graphs Modified Part A - Terminology and Traversals

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

Page 15: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

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)

15 - 15

Page 16: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

Weighted Graphs• We could use an undirected network to

represent flights between cities• The weights are the cost of flight

15 - 16

Page 17: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

Weighted Graphs• A directed version of the graph could show

different costs depending on the direction

15 - 17

Page 18: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

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)

15 - 18

Page 19: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

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

15 - 19

Page 20: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

Graph Traversals

15 - 20

Page 21: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

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

15 - 21

Page 22: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

DFT starting from vertex vcreate a stack Svisit, mark v as visited and push v onto Swhile S is non-empty

peek at the top u of Sif u has an (unvisited) neighbor w,

visit w, mark w and push it onto Selse pop S

15 - 22

Page 23: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Chapter 15

Graphs

Modified

Part B – Data Structures for Graphs

Page 24: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

Basic Data Structures for Graphs

• Adjacency lists• Adjacency matrices

15 - 24

Page 25: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase 15 - 25

Page 26: Chapter 15 Graphs Modified Part A - Terminology and Traversals

15 - 26

For weighted graph - network

Can add weight field to nodes in adjacency lists.

Page 27: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Chapter 15

Graphs

Modified

Part C – Graph Algorithms 1

Page 28: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

Basic Graph Algorithms

• Un-weighted graphs/digraphs–Connectivity–Cycle detection–Shortest path–Spanning tree–Topological sort

15 - 28

Page 29: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

Basic Graph Algorithms

• Weighted graphs/digraphs–Minimum spanning tree–Cheapest path–Traveling salesman problem

15 - 29

Page 30: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

Dynamic graphs

• Adding and removing vertices• Adding and removing edges

15 - 30

Page 31: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase 15 - 31

Page 32: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

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.

15 - 32

Page 33: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase 15 - 33

Page 34: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

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.

15 - 34

Page 35: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

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

15 - 35

Page 36: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

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

15 - 36

Page 37: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase 15 - 37

Page 38: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Chapter 15

Graphs

Modified

Part D – Graph Algorithms 2

Page 39: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

Basic Graph Algorithms

• Un-weighted graphs/digraphs–Spanning tree–Topological sort

15 - 39

Page 40: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase 15 - 40

Page 41: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

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.15 - 41

Page 42: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

Finding a spanning tree

• Do a depth first traversal and record all edges traversed to get to a “new” edge.

15 - 42

Page 43: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

Find Spanning tree – DFT starting from vertex v

create a stack SInitialize spanning tree edge list to emptymark v as visited and push v onto Swhile S is non-empty

peek at the top u of Sif u has an (unvisited) neighbor w,

add edge from u to w to spanning tree, mark w and push it onto S

else pop S15 - 43

Page 44: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase 15 - 44

Page 45: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

Topological sortA 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.

15 - 45

Page 47: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

Alternate algorithm for top. sort of DAG

1. Compute the indegrees of all vertices2. Find a vertex U with indegree 0 and append it to

the ordering3. 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.

15 - 47

Page 48: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Chapter 15

Graphs

Modified

Part E – Graph Algorithms 3

Page 49: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

Basic Graph Algorithms

• Weighted graphs/digraphs–Minimum spanning tree–Cheapest path–Traveling salesman problem

15 - 49

Page 50: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase 15 - 50

Page 51: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

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.15 - 51

Page 52: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

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.

15 - 52

Page 53: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

Prim’s algorithm1. 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).

15 - 53

https://www.cs.usfca.edu/~galles/visualization/Algorithms.html

Page 54: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

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.

15 - 54

Page 55: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

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.

15 - 55

Page 56: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

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.

15 - 56

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.

Page 57: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase 15 - 57

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.

Page 58: Chapter 15 Graphs Modified Part A - Terminology and Traversals

CN5E by Tanenbaum & Wetherall, © Pearson Education-Prentice Hall and D. Wetherall, 2011

First 5 steps in computing the shortest paths from A.

Page 59: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

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.

15 - 59

Page 61: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

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?

15 - 61

Page 62: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

Brute Force Solutions of TSP

15 - 62

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.

Page 63: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase 15 - 63

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.

Page 64: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase 15 - 64

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.

Page 65: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase 15 - 65

TSP is an NP-hard problem-Comp 314/Comp 332

Page 66: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Chapter 15

Graphs

Modified

Part F – Graph Algorithms 4

Page 67: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

Dynamic graphs

• Adding and removing vertices• Adding and removing edges

15 - 67

Page 68: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase 15 - 68

Page 69: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

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

15 - 69

Page 70: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

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.

15 - 70

Page 71: Chapter 15 Graphs Modified Part A - Terminology and Traversals

Java Software Structures, 4th Edition, Lewis/Chase

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.

15 - 71