0
Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# Ch18

26,152

Published on

Published in: Technology
1 Comment
3 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Views
Total Views
26,152
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
34,163
1
Likes
3
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. Chapter 18 Graphs
• 2. Chapter Objectives
• Define undirected graphs
• Define directed graphs
• Define weighted graphs or networks
• Explore common graph algorithms
• 3. Graphs
• Like a tree, a graph is made up of nodes and the connections between those nodes
• In graph terminology, we refer to the nodes a vertices and the connections as edges
• Vertices are typically referred to by label (e.g. A, B, C, D)
• Edges are referenced by a paring of vertices (e.g. (A, B) represent an edge between A and B)
• 4. Undirected Graphs
• An undirected graph is a graph where the pairings representing edges are unordered
• Listing an edge as (A, B) means that there is an edge between A and B that can traversed in either direction
• For an undirected graph, (A, B) means exactly the same thing as (B, A)
• 5. FIGURE 18.1 An example undirected graph
• 6. Undirected Graphs
• Two vertices in a graph are 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 a sling
• An undirected graph is considered complete if it has the maximum number of edges connecting vertices (n(n-1)/2)
• 7. Undirected Graphs
• A path is a sequence of edges that connects two vertices in a graph
• A, B, D is a path from A to D in our previous example
• The length of a path is the number of edges in the path (number of vertices - 1)
• An undirected graph is considered connected if for any two vertices in the graph, there is a path between them
• The graph in our previous example is connected
• The following graph is not connected
• 8. FIGURE 18.2 An example undirected graph that is not connected
• 9. Undirected Graphs
• A cycle is a path in which the first and last vertices are repeated
• For example, in the previous slide, A, B, C, A is a cycle
• A graph that has no cycles is called acyclic
• An undirected tree is a connected, acyclic, undirected graph with one element designated as the root
• 10. Directed Graphs
• A directed graph , or digraph , is a graph where the edges are ordered pairs of vertices
• This means that the edge (A, B) and (B, A) are separate, directional edges
• Figure 18.1 was described as:
• Vertices: A, B, C, D
• Edges: (A, B), (A, C), (B, C), (B, D), (C, D)
• Interpreting this as a directed graph yields the graph in Figure 18.3
• 11. FIGURE 18.3 An example directed graph
• 12. FIGURE 18.4 Connected and Unconnected Directed Graphs
• 13. Directed Graphs
• If a directed graph has no cycles, it is possible to arrange the vertices such that vertex A precedes vertex B if an edge exists from A to B
• This order of vertices is called topological order
• A directed tree is a directed graph that has an element designated as the root and has the following properties
• There are no connections from other vertices to the root
• Every non-root element has exactly on connection to it
• There is a path from the root to every other vertex
• 14. Networks
• A network or a weighted graph is a graph with weights or costs associated with each edge
• Figure 18.5 shows an undirected network of the connections and airfares between cities
• Networks may be directed or undirected
• Figure 18.6 shows a directed network
• 15. FIGURE 18.5 A network, or weighted graph
• 16. FIGURE 18.6 A directed network
• 17. Networks
• For networks, we represent each edge with a triple including the starting vertex, the ending vertex, and the weight
• (Boston, New York, 120)
• 18. Common Graph Algorithms
• For trees, we defined four types of traversals
• Generally, we divide graph traversals into two categories
• Depth-first traversal
• 19. Common Graph Algorithms
• We can construct a breadth-first traversal for a graph similarly to our level-order traversal of a tree
• Use a queue and an unordered list
• We use the queue to manage the traversal
• We use the unordered list to build our result
• 20. Listing 18.1
• 21. Listing 18.1 (cont.)
• 22. Common Graph Algorithms
• We can construct a depth-first traversal for a graph similarly to our level-order traversal of a tree by replacing the queue with a stack
• Use a stack and an unordered list
• We use the stack to manage the traversal
• We use the unordered list to build our result
• 23. Listing 18.2
• 24. Listing 18.2 (cont.)
• 25. Common Graph Algorithms
• Of course, both of these algorithms could be expressed recursively
• 26. Common Graph Algorithms
• Another common graph algorithm it testing for connectivity
• The graph is connected if and only if for each vertex v in a graph containing n vertices, the size of the result of a breadth-first traversal starting a v is n
• 27. FIGURE 18.8 Connectivity in an undirected graph
• 28. TABLE 18.1 Breadth-first traversals for a connected undirected graph
• 29. TABLE 18.2 Breadth-first traversals for an unconnected undirected graph
• 30. Spanning Trees
• A spanning tree is a tree that includes all of the vertices of a graph
• The following example shows a graph and then a spanning tree for that graph
• 31. FIGURE 18.7 A sample graph
• 32. FIGURE 18.9 A spanning tree for the graph in Figure 18.7
• 33. Minimum Spanning Trees
• A minimum spanning tree is a spanning tree where the sum of the weights of the edges is less than or equal to the sum of the weights for any other spanning tree for the same graph
• The algorithm for creating a minimum spanning tree makes use of a minheap to order the edges
• 34. FIGURE 18.10 A minimum spanning tree
• 35. Listing 18.3
• 36. Listing 18.3 (cont.)
• 37. Listing 18.3 (cont.)
• 38. Determining the Shortest Path
• There are two possibilities for determining the shortest path in a graph
• Determine the literal shortest path in terms of the number of edges
• Determine the least expensive path in a network
• 39. Determining the Shortest Path
• The solution to the first of these is a simple variation of our earlier breadth-first traversal algorithm
• We simply store two additional pieces of information for each vertex
• The path length from the starting point to this vertex
• The vertex that is the predecessor of this vertex on that path
• Then we modify our loop to terminate when we reach our target vertex
• 40. Determining the Shortest Path
• The second possibility is to look for the cheapest path in a network
• Dijkstra develop an algorithm for this possibility that is similar to our previous algorithm
• However, instead of using a queue of vertices, we use a minheap or a priority queue storing vertex, weight pairs based upon total weight
• Thus we always traverse through the graph following the cheapest path first
• 41. Strategies for Implementing Graphs
• There are two principle approaches to implementing graphs