2.
Chapter Objectives <ul><li>Define undirected graphs </li></ul><ul><li>Define directed graphs </li></ul><ul><li>Define weighted graphs or networks </li></ul><ul><li>Explore common graph algorithms </li></ul>
3.
Graphs <ul><li>Like a tree, a graph is made up of nodes and the connections between those nodes </li></ul><ul><li>In graph terminology, we refer to the nodes a vertices and the connections as edges </li></ul><ul><li>Vertices are typically referred to by label (e.g. A, B, C, D) </li></ul><ul><li>Edges are referenced by a paring of vertices (e.g. (A, B) represent an edge between A and B) </li></ul>
4.
Undirected Graphs <ul><li>An undirected graph is a graph where the pairings representing edges are unordered </li></ul><ul><li>Listing an edge as (A, B) means that there is an edge between A and B that can traversed in either direction </li></ul><ul><li>For an undirected graph, (A, B) means exactly the same thing as (B, A) </li></ul>
6.
Undirected Graphs <ul><li>Two vertices in a graph are adjacent if there is an edge connecting them </li></ul><ul><li>Adjacent vertices are sometimes referred to as neighbors </li></ul><ul><li>An edge of a graph that connects a vertex to itself is called a self-loop or a sling </li></ul><ul><li>An undirected graph is considered complete if it has the maximum number of edges connecting vertices (n(n-1)/2) </li></ul>
7.
Undirected Graphs <ul><li>A path is a sequence of edges that connects two vertices in a graph </li></ul><ul><ul><li>A, B, D is a path from A to D in our previous example </li></ul></ul><ul><li>The length of a path is the number of edges in the path (number of vertices - 1) </li></ul><ul><li>An undirected graph is considered connected if for any two vertices in the graph, there is a path between them </li></ul><ul><ul><li>The graph in our previous example is connected </li></ul></ul><ul><ul><li>The following graph is not connected </li></ul></ul>
8.
FIGURE 18.2 An example undirected graph that is not connected
9.
Undirected Graphs <ul><li>A cycle is a path in which the first and last vertices are repeated </li></ul><ul><ul><li>For example, in the previous slide, A, B, C, A is a cycle </li></ul></ul><ul><li>A graph that has no cycles is called acyclic </li></ul><ul><li>An undirected tree is a connected, acyclic, undirected graph with one element designated as the root </li></ul>
10.
Directed Graphs <ul><li>A directed graph , or digraph , is a graph where the edges are ordered pairs of vertices </li></ul><ul><li>This means that the edge (A, B) and (B, A) are separate, directional edges </li></ul><ul><li>Figure 18.1 was described as: </li></ul><ul><ul><li>Vertices: A, B, C, D </li></ul></ul><ul><ul><li>Edges: (A, B), (A, C), (B, C), (B, D), (C, D) </li></ul></ul><ul><li>Interpreting this as a directed graph yields the graph in Figure 18.3 </li></ul>
12.
FIGURE 18.4 Connected and Unconnected Directed Graphs
13.
Directed Graphs <ul><li>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 </li></ul><ul><li>This order of vertices is called topological order </li></ul><ul><li>A directed tree is a directed graph that has an element designated as the root and has the following properties </li></ul><ul><ul><li>There are no connections from other vertices to the root </li></ul></ul><ul><ul><li>Every non-root element has exactly on connection to it </li></ul></ul><ul><ul><li>There is a path from the root to every other vertex </li></ul></ul>
14.
Networks <ul><li>A network or a weighted graph is a graph with weights or costs associated with each edge </li></ul><ul><li>Figure 18.5 shows an undirected network of the connections and airfares between cities </li></ul><ul><li>Networks may be directed or undirected </li></ul><ul><li>Figure 18.6 shows a directed network </li></ul>
17.
Networks <ul><li>For networks, we represent each edge with a triple including the starting vertex, the ending vertex, and the weight </li></ul><ul><ul><li>(Boston, New York, 120) </li></ul></ul>
18.
Common Graph Algorithms <ul><li>For trees, we defined four types of traversals </li></ul><ul><li>Generally, we divide graph traversals into two categories </li></ul><ul><ul><li>Breadth-first traversal </li></ul></ul><ul><ul><li>Depth-first traversal </li></ul></ul>
19.
Common Graph Algorithms <ul><li>We can construct a breadth-first traversal for a graph similarly to our level-order traversal of a tree </li></ul><ul><ul><li>Use a queue and an unordered list </li></ul></ul><ul><ul><li>We use the queue to manage the traversal </li></ul></ul><ul><ul><li>We use the unordered list to build our result </li></ul></ul>
22.
Common Graph Algorithms <ul><li>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 </li></ul><ul><ul><li>Use a stack and an unordered list </li></ul></ul><ul><ul><li>We use the stack to manage the traversal </li></ul></ul><ul><ul><li>We use the unordered list to build our result </li></ul></ul>
25.
Common Graph Algorithms <ul><li>Of course, both of these algorithms could be expressed recursively </li></ul>
26.
Common Graph Algorithms <ul><li>Another common graph algorithm it testing for connectivity </li></ul><ul><li>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 </li></ul>
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 <ul><li>A spanning tree is a tree that includes all of the vertices of a graph </li></ul><ul><li>The following example shows a graph and then a spanning tree for that graph </li></ul>
32.
FIGURE 18.9 A spanning tree for the graph in Figure 18.7
33.
Minimum Spanning Trees <ul><li>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 </li></ul><ul><li>The algorithm for creating a minimum spanning tree makes use of a minheap to order the edges </li></ul>
38.
Determining the Shortest Path <ul><li>There are two possibilities for determining the shortest path in a graph </li></ul><ul><ul><li>Determine the literal shortest path in terms of the number of edges </li></ul></ul><ul><ul><li>Determine the least expensive path in a network </li></ul></ul>
39.
Determining the Shortest Path <ul><li>The solution to the first of these is a simple variation of our earlier breadth-first traversal algorithm </li></ul><ul><li>We simply store two additional pieces of information for each vertex </li></ul><ul><ul><li>The path length from the starting point to this vertex </li></ul></ul><ul><ul><li>The vertex that is the predecessor of this vertex on that path </li></ul></ul><ul><li>Then we modify our loop to terminate when we reach our target vertex </li></ul>
40.
Determining the Shortest Path <ul><li>The second possibility is to look for the cheapest path in a network </li></ul><ul><li>Dijkstra develop an algorithm for this possibility that is similar to our previous algorithm </li></ul><ul><li>However, instead of using a queue of vertices, we use a minheap or a priority queue storing vertex, weight pairs based upon total weight </li></ul><ul><li>Thus we always traverse through the graph following the cheapest path first </li></ul>
41.
Strategies for Implementing Graphs <ul><li>There are two principle approaches to implementing graphs </li></ul><ul><ul><li>Adjacency lists </li></ul></ul><ul><ul><li>Adjacency matrices </li></ul></ul><ul><li>The adjacency list approach is modeled closely to the way we implemented linked implementations of trees </li></ul><ul><li>However, instead of building a graph node that contains a fixed number of references (as we did with BinaryTreeNode) we will build a graph node that simply maintain a linked lists of references to other nodes </li></ul><ul><li>This list is called an adjacency list </li></ul>
42.
Strategies for Implementing Graphs <ul><li>The second strategy for implementing graphs is to use an adjacency matrix </li></ul><ul><li>An adjacency matrix is simply a two-dimensional array where both dimensions are “indexed” by the vertices of the graph </li></ul><ul><li>Each position in the matrix contains a boolean that is true if the two associated vertices are connected by an edge, and false otherwise </li></ul><ul><li>The following slides show two example of adjacency matrices, one for an undirected graph, the other for a directed graph </li></ul><ul><li>An adjacency matrix for a network could store the weights in each cell instead of a boolean </li></ul>
Clipping is a handy way to collect and organize the most important slides from a presentation. You can keep your great finds in clipboards organized around topics.