Download presentation
Presentation is loading. Please wait.
Published byRidwan Sutedja Modified over 6 years ago
1
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
2
Overview 15.1 15.2 15.3 Discussion of graph terminology.
Representation 15.3 Traversal
3
Overview 15.5 15.6 Algorithms for finding shortest paths
Minimum spanning tree Graphs generalize trees. Graphs may have more than one path between two nodes.
4
Terminology Graph Consists of vertices (nodes) and edges.
Vertices are circles. Edges are lines or arrows.
5
Terminology Directed graph Undirected graph
Edge is an arrow going from one vertex to another. In some situations, it is meaningful to have a self- loop. Undirected graph Edge is simply an line connecting two vertices. Undirected graphs usually do not have self-loops. For every undirected graph there is a corresponding directed graph where each undirected edge is replaced by two directed edges, one in each direction.
6
Terminology
7
Terminology Neighbors Path Length
Vertices that can be reached by moving along one edge. Path A sequence of vertices, each a neighbor of the previous one. Length Length of the path is the number of edges along the path.
8
Terminology Distance Cycle Weighted graphs
Distance between two vertices is the length of the shortest path between them. Cycle A path (of length one or more) from a vertex back to itself. Weighted graphs Data associated with the edges.
9
Terminology
10
Terminology
11
Terminology
12
Terminology
13
Terminology A graph with no cycles is called acyclic. dag
Directed acyclic graph
14
Terminology A graph in which every pair of vertices is connected by a path is said to be connected.
15
Terminology Weighted graph. We can associate data with an edge.
16
Terminology When analyzing graph algorithms, we often give results in terms of v (the number of vertices) and e (the number of edges). In a directed graph, e ≤ v2. In an undirected graph:
17
Terminology In either case e O(v2) Dense Sparse
A graph with close to the maximum number of edges. Sparse A graph with far fewer edges.
18
Representation Neighbor list or adjacency list
The amount of memory used is in Θ(v + e). The time to check for a particular edge is in O(e).
19
Representation If a graph is dense, it is better to use a neighbor matrix or adjacency matrix. This is essentially a two-dimensional array of booleans. <i, j> is true when there is an edge from vertex i to vertex j. Memory used is Θ(n2). The time to check for a particular edge is constant. Easily adapted to handle weighted graphs. 0 if there is no edge.
20
Representation
21
Representation
22
Representation
23
Representation
24
Representation
25
Representation
26
Representation
27
Representation
28
Representation
29
Representation
30
Representation
31
Graph Traversal Source vertex Depth-first traversal Breadth-first
We must choose an arbitrary vertex to start from. Depth-first traversal Follows edges until it reaches a dead end, then backtracks to the last branching point. Breadth-first Visits one vertex, then its neighbors, then vertices two edges away, and so on.
32
Graph Traversal Vertices that cannot be reached from the source are never visited. It is necessary to keep track of which vertices have already been visited. This is done with an array of booleans.
33
Graph Traversal Depth-first
34
Graph Traversal Breadth-first
35
Graph Traversal
36
Graph Traversal
37
Graph Traversal
38
Shortest Paths A common graph problem is finding the shortest path between two vertices. We want to minimize the sum of the weghts of the edges on the path, not the number of vertices. How much does it cost to get directly from one vertex to another. Weight of the edge is the cost. If they are not connected, the cost is infinite. Double.POSTITIVE_INFINITY Double.POSTITIVE_INFINITY + 1 == Double.POSTIVITE_INFINITY
39
Shortest Paths
40
Shortest Paths Two algorithms
Dijkstra's single source algorithm determines the distances from one vertex to all others. Floyd-Warshall all pairs algorithm, which might be used to create a table of distances in a road atlas, determines the distance from each vertex to each other vertex.
41
Shortest Paths Dijkstra's Single-Source Algorithm
Maintains an array containing the distance to each vertex. Initially, the distance to the source vertex is 0 and all other distances are infinite.
42
Shortest Paths The vertices are visited in order of increasing distance from the source. By the time we visit a vertex, we have visited all of the vertices along the shortest path to that vertex. Similar to a breadth-first traversal, but the weights are taken into account. result[i] + getCost(i, j) If this sum is smaller, we have found a shorter path to j, so we update result[j] Total running time Θ(v2)
43
Shortest Paths
44
Shortest Paths
45
Shortest Paths Floyd-Warshall All-Pairs Algorithm
To find the shorest path between each pair of vertices, we could just run Dijkstra's algorithm once from each vertex, using time in Θ(v3) Floyd-Warshall all-pairs takes time in this order, but it is somewhat simpler. Uses dynamic programming. result[i] [j] is the shortest known distance from vertex i to vertex j.
46
Shortest Paths Questions to ask:
What is the shortest distance between each pair of vertices using vertex 0 as an intermediate point? What is the shortest distance between each pair of vertices using vertices 0 and 1 as intermediate points? What is the shortest distance between each pair of vertices using vertices 0 through 2 as intermediate points?
47
Shortest Paths When possible intermediate points have been considered the distances are correct. Two possibilities for each pair of vertices i and j: The shortest path using vertices 0 through 5 as intermediate points does not involve vertex 5 (It was already correct). The shortest path using vertices 0 through 5 as intermediate points does involve vertex 5. It must be result[i][5] + result[5][j]. Neither of these two subpaths can have vertex 5 as an intermediate point, because it is an endpoint. Running time in Θ(v3)
48
Shortest Paths
49
Shortest Paths
50
Minimum Spanning Trees
Connect a set of vertices while minimizing expenses. Spanning tree Given a connected, undirected graph G, a spanning tree is another graph which has all of G's vertices and a subset of G's edges Spanning tree has only v – 1 edges. Minimum number of edges required to connect all of the vertices. Never contains a cycle. May be more than one spanning tree for a given graph.
51
Minimum Spanning Trees
In a weighted graph, a minimum spanning tree is a spanning tree in which the sum of the edge weight is as small as possible. There may be more than one minimum spanning tree for a given graph. Kruskal's minimum spanning tree algorithm. Begin with an edgeless graph, then add edges until we have a minimum spanning tree. Add the cheapest edge that we haven't tried before. If there was not already a path between the vertices at the ends of the edge, we add the edge.
52
Minimum Spanning Trees
53
Minimum Spanning Trees
54
Minimum Spanning Trees
55
Minimum Spanning Trees
56
Minimum Spanning Trees
This is a Greedy algorithm Always does whatever seems best in the short term.
57
Minimum Spanning Trees
58
Minimum Spanning Trees
59
Minimum Spanning Trees
60
Minimum Spanning Trees
61
Minimum Spanning Trees
Total running time:
62
Summary A graph is a collection of vertices connected by edges.
Directed or undirected, weighted or unweighted, and connected or unconnected. A graph with no cycles is called acyclic. Graphs are normally represented using neighbor lists or neighbor matrices. Neighbor lists use less space for sparse graphs. Neighbor matrices take less time in either case.
63
Summary A graph may be traversed depth-first.
A topological sort of a directed acyclic graph is an ordering of the vertices such that no edges point from a later vertex to an earlier vertex. Finding shortest paths in a weighted graph eliminates some edges so as to stay connected while minimizing the total edge weight. Kruskal's minimum spanning tree algorithm is a greedy algorithm.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.