Download presentation
Presentation is loading. Please wait.
Published byElizabeth Owens Modified over 9 years ago
1
All leaves are on the bottom level. All internal nodes (except perhaps the root node) have at least ceil(m / 2) (nonempty) children. The root node can have as few as 2 children if it is an internal node, and can obviously have no children if the root node is a leaf (that is, the whole tree consists only of the root node). Each leaf node (other than the root node if it is a leaf) must contain at least ceil(m / 2) - 1 keys. A B-tree of order m is a multiway search tree of order m such that: Note that ceil(x) is the so-called ceiling function. It's value is the smallest integer that is greater than or equal to x. Thus ceil(3) = 3, ceil(3.35) = 4, ceil(1.98) = 2, ceil(5.01) = 6, ceil(7) = 7, etc. A B-tree is a fairly well-balanced tree by virtue of the fact that all leaf nodes must be at the bottom. Condition (2) tries to keep the tree fairly bushy by insisting that each node have at least half the maximum number of children. This causes the tree to "fan out" so that the path from root to leaf is very short even in a tree that contains a lot of data.
2
Let's work our way through an example similar to that given by Kruse. Insert the following letters into what is originally an empty B-tree of order 5: C N G A H E K Q M F W L T Z D P R X Y S Order 5 means that a node can have a maximum of 5 children and 4 keys. All nodes other than the root must have a minimum of 2 keys. The first 4 letters get inserted into the same node, resulting in this picture:
3
When we try to insert the H, we find no room in this node, so we split it into 2 nodes, moving the median item G up into a new root node. Note that in practice we just leave the A and C in the current node and place the H and N into a new node to the right of the old one. Inserting E, K, and Q proceeds without requiring any splits:
4
Inserting M requires a split. Note that M happens to be the median key and so is moved up into the parent node. The letters F, W, L, and T are then added without needing any split.
5
When Z is added, the rightmost leaf must be split. The median item T is moved up into the parent node. Note that by moving up the median key, the tree is kept fairly balanced, with 2 keys in each of the resulting nodes. The insertion of D causes the leftmost leaf to be split. D happens to be the median key and so is the one moved up into the parent node. The letters P, R, X, and Y are then added without any need of splitting:
6
Finally, when S is added, the node with N, P, Q, and R splits, sending the median Q up to the parent. However, the parent node is full, so it splits, sending the median M up to form a new root node. Note how the 3 pointers from the old parent node stay in the revised node that contains D and G.
7
Deleting an Item In the B-tree as we left it at the end of the last section, delete H. Of course, we first do a lookup to find H. Since H is in a leaf and the leaf has more than the minimum number of keys, this is easy. We move the K over where the H had been and the L over where the K had been. This gives
8
Next, delete the T. Since T is not in a leaf, we find its successor (the next item in ascending order), which happens to be W, and move W up to replace the T. That way, what we really have to do is to delete W from the leaf, which we already know how to do, since this leaf has extra keys. In ALL cases we reduce deletion to a deletion in a leaf, by using this method Next, delete R. Although R is in a leaf, this leaf does not have an extra key; the deletion results in a node with only one key, which is not acceptable for a B-tree of order 5. If the sibling node to the immediate left or right has an extra key, we can then borrow a key from the parent and move a key up from this sibling. In our specific case, the sibling to the right has an extra key. So, the successor W of S (the last key in the node where the deletion occurred), is moved down from the parent, and the X is moved up. (Of course, the S is moved over so that the W can be inserted in its proper place.)
10
Finally, let's delete E. This one causes lots of problems. Although E is in a leaf, the leaf has no extra keys, nor do the siblings to the immediate right or left. In such a case the leaf has to be combined with one of these two siblings. This includes moving down the parent's key that was between those of these two leaves. In our example, let's combine the leaf containing F with the leaf containing A C. We also move down the D.
11
We begin by finding the immediate successor, which would be D, and move the D up to replace the C. However, this leaves us with a node with too few keys.
14
Topological Sort Introduction. Definition of Topological Sort. Topological Sort is Not Unique. Topological Sort Algorithm. An Example. Implementation. Review Questions.
15
Introduction There are many problems involving a set of tasks in which some of the tasks must be done before others. For example, consider the problem of taking a course only after taking its prerequisites. Is there any systematic way of linearly arranging the courses in the order that they should be taken? Yes! - Topological sort.
16
Definition of Topological Sort Topological sort is a method of arranging the vertices in a directed acyclic graph (DAG), as a sequence, such that no vertex appear in the sequence before its predecessor. The graph in (a) can be topologically sorted as in (b) (a)(b)
17
Topological Sort is not unique Topological sort is not unique. The following are all topological sort of the graph below: s1 = {a, b, c, d, e, f, g, h, i} s2 = {a, c, b, f, e, d, h, g, i} s3 = {a, b, d, c, e, g, f, h, i} s4 = {a, c, f, b, e, h, d, g, i} etc.
18
Topological Sort Algorithm One way to find a topological sort is to consider in-degrees of the vertices. The first vertex must have in-degree zero -- every DAG must have at least one vertex with in-degree zero. The Topological sort algorithm is: int topologicalOrderTraversal( ){ int numVisitedVertices = 0; while(there are more vertices to be visited){ if(there is no vertex with in-degree 0) break; else{ select a vertex v that has in-degree 0; visit v; numVisitedVertices++; delete v and all its emanating edges; } return numVisitedVertices; }
19
Topological Sort Example Demonstrating Topological Sort. A F B G C H D I E J 1 2 3 02 10 2 20 D G A B F H J E I C
20
Implementation of Topological Sort The algorithm is implemented as a traversal method that visits the vertices in a topological sort order. An array of length |V| is used to record the in-degrees of the vertices. Hence no need to remove vertices or edges. A priority queue is used to keep track of vertices with in-degree zero that are not yet visited. public int topologicalOrderTraversal(Visitor visitor){ int numVerticesVisited = 0; int[] inDegree = new int[numberOfVertices]; for(int i = 0; i < numberOfVertices; i++) inDegree[i] = 0; Iterator p = getEdges(); while (p.hasNext()) { Edge edge = (Edge) p.next(); Vertex to = edge.getToVertex(); inDegree[getIndex(to)]++; }
21
Implementation of Topological Sort BinaryHeap queue = new BinaryHeap(numberOfVertices); p = getVertices(); while(p.hasNext()){ Vertex v = (Vertex)p.next(); if(inDegree[getIndex(v)] == 0) queue.enqueue(v); } while(!queue.isEmpty() && !visitor.isDone()){ Vertex v = (Vertex)queue.dequeueMin(); visitor.visit(v); numVerticesVisited++; p = v.getSuccessors(); while (p.hasNext()){ Vertex to = (Vertex) p.next(); if(--inDegree[getIndex(to)] == 0) queue.enqueue(to); } return numVerticesVisited; }
22
Review Questions 1. List the order in which the nodes of the directed graph GB are visited by topological order traversal that starts from vertex a. 2. What kind of DAG has a unique topological sort? 3. Generate a directed graph using the required courses for your major. Now apply topological sort on the directed graph you obtained.
23
What is a Graph? A graph G = (V,E) is composed of: V: set of vertices E: set of edges connecting the vertices in V An edge e = (u,v) is a pair of vertices Example: a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d), (b,e),(c,d),(c,e), (d,e)}
24
Applications electronic circuits networks (roads, flights, communications) CS16 LAX JFK LAX DFW STL HNL FTL
25
Terminology: Adjacent and Incident If (v 0, v 1 ) is an edge in an undirected graph, – v 0 and v 1 are adjacent – The edge (v 0, v 1 ) is incident on vertices v 0 and v 1 If is an edge in a directed graph – v 0 is adjacent to v 1, and v 1 is adjacent from v 0 – The edge is incident on v 0 and v 1
26
The degree of a vertex is the number of edges incident to that vertex For directed graph, the in-degree of a vertex v is the number of edges that have v as the head the out-degree of a vertex v is the number of edges that have v as the tail if di is the degree of a vertex i in a graph G with n vertices and e edges, the number of edges is Terminology: Degree of a Vertex Why? Since adjacent vertices each count the adjoining edge, it will be counted twice
27
0 12 3 4 5 6 G1G1 G2G2 3 2 33 1 1 1 1 directed graph in-degree out-degree 0 1 2 G3G3 in:1, out: 1 in: 1, out: 2 in: 1, out: 0 0 12 3 3 3 3 Examples
28
28 Terminology: Path path: sequence of vertices v 1,v 2,...v k such that consecutive vertices v i and v i+1 are adjacent. 3 3 3 3 2 a b c d e a b c d e a b e d cb e d c
29
More Terminology simple path: no repeated vertices cycle: simple path, except that the last vertex is the same as the first vertex a b c d e b e c
30
Even More Terminology subgraph: subset of vertices and edges forming a graph connected component: maximal connected subgraph. E.g., the graph below has 3 connected components. connectednot connected connected graph: any two vertices are connected by some path
31
00 123 120 12 3 (i) (ii) (iii) (iv) (a) Some of the subgraph of G 1 0 0 1 0 1 2 0 1 2 (i) (ii) (iii) (iv) (b) Some of the subgraph of G 3 0 12 3 G1G1 0 1 2 G3G3 Subgraphs Examples
32
More… tree - connected graph without cycles forest - collection of trees
33
Directed vs. Undirected Graph An undirected graph is one in which the pair of vertices in a edge is unordered, (v 0, v 1 ) = (v 1,v 0 ) A directed graph is one in which each edge is a directed pair of vertices, != tail head
34
Graph Representations Adjacency Matrix Adjacency Lists
35
Adjacency Matrix Let G=(V,E) be a graph with n vertices. The adjacency matrix of G is a two-dimensional n by n array, say adj_mat If the edge (v i, v j ) is in E(G), adj_mat[i][j]=1 If there is no such edge in E(G), adj_mat[i][j]=0 The adjacency matrix for an undirected graph is symmetric; the adjacency matrix for a digraph need not be symmetric
36
Examples for Adjacency Matrix G1G1 G2G2 G4G4 0 12 3 0 1 2 1 0 2 3 4 5 6 7 symmetric undirected: n 2 /2 directed: n 2
37
01230123 012012 0123456701234567 1 23 023 013 0 12 G1G1 1 0 2 G3G3 1 2 0 3 0 3 12 5 4 6 57 6 G4G4 0 12 3 0 1 2 1 0 2 3 4 5 6 7 An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes
38
Depth-First Search38 DFS : Depth-First Search DFS is another popular search strategy. It can do certain things that BFS cannot do. We will discuss some of these algorithms in COMP 271 (so you cannot get rid of DFS after COMP171). DFS idea : Whenever we visit a vertex v from another vertex u, we recursively visit a neighbor of v that has not been visited before until all neighbors of v have been visited. Then we backtrack (return) to u.
39
Depth-First Search39 Algorithm Flag all vertices as not visited Visit v, and mark v as visited. For each unvisited neighbor. make a recursive call RDFS(w).
40
Depth-First Search40 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) F F F F F F F F F F Initialize visited table (all empty F) Initialize Pred to -1 - - - - - - - - - - Pred
41
Depth-First Search41 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) F F T F F F F F F F Mark 2 as visited - - -1 - - - - - - - Pred RDFS( 2 ) recursive call RDFS(8) visit sequence= {2}
42
Depth-First Search42 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) F F T F F F F F T F Mark 8 as visited - - -1 - - - - - 2 - Pred RDFS( 2 ) RDFS(8) recursive call RDFS(0) Recursive calls visit sequence= {2, 8}
43
Depth-First Search43 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T F T F F F F F T F Mark 0 as visited 8 - -1 - - - - - 2 - Pred RDFS( 2 ) RDFS(8) RDFS(0) -> no unvisited neighbor, return to (backtrack) RDFS(8) Recursive calls visit sequence= {2, 8, 0}
44
Depth-First Search44 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T F T F F F F F T F 8 - -1 - - - - - 2 - Pred RDFS( 2 ) RDFS(8) recursive call RDFS(9) Recursive calls Backtrack to 8 visit sequence= {2, 8, 0}
45
Depth-First Search45 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T F T F F F F F T T Mark 9 as visited 8 - -1 - - - - - 2 8 Pred RDFS( 2 ) RDFS(8) RDFS(9) recursive call RDFS(1) Recursive calls visit sequence= {2, 8, 0, 9}
46
Depth-First Search46 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T F F F F F T T Mark 1 as visited 8 9 -1 - - - - - 2 8 Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) recursive call RDFS(3) Recursive calls visit sequence= {2, 8, 0, 9, 1}
47
Depth-First Search47 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T F F F F T T Mark 3 as visited 8 9 -1 1 - - - - 2 8 Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) recursive call RDFS(4) Recursive calls visit sequence= {2, 8, 0, 9, 1, 3}
48
Depth-First Search48 RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) RDFS(4) STOP all of 4’s neighbors have been visited backtrack (return back) to call RDFS(3) Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T F F F T T Mark 4 as visited 8 9 -1 1 3 - - - 2 8 Pred Recursive calls visit sequence= {2, 8, 0, 9, 1, 3, 4}
49
Depth-First Search49 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T F F F T T 8 9 -1 1 3 - - - 2 8 Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) recursive call RDFS(5) Recursive calls Backtrack to 3 visit sequence= {2, 8, 0, 9, 1, 3, 4}
50
Depth-First Search50 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T T F F T T 8 9 -1 1 3 3 - - 2 8 Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) RDFS(5) 3 is visited, recursive call RDFS(6) Recursive calls Mark 5 as visited visit sequence= {2, 8, 0, 9, 1, 3, 4, 5}
51
Depth-First Search51 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T T T F T T 8 9 -1 1 3 3 5 - 2 8 Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) RDFS(5) RDFS(6) recursive call RDFS(7) Recursive calls Mark 6 as visited visit sequence= {2, 8, 0, 9, 1, 3, 4, 5, 6}
52
Depth-First Search52 Example 2 4 3 5 1 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T T T T T T 8 9 -1 1 3 3 5 6 2 8 Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) RDFS(5) RDFS(6) RDFS(7) Recursive calls Mark 7 as visited visit sequence= {2, 8, 0, 9, 1, 3, 4, 5, 6, 7} 7
53
Depth-First Search53 Example 2 4 3 5 1 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T T T T T T 8 9 -1 1 3 3 5 6 2 8 Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) RDFS(5) RDFS(6) RDFS(7) no recursive call Recursive calls visit sequence= {2, 8, 0, 9, 1, 3, 4, 5, 6, 7} 7
54
Depth-First Search54 Example 2 4 3 5 1 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T T T T T T 8 9 -1 1 3 3 5 6 2 8 Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) RDFS(5) RDFS(6) no recursive call Recursive calls visit sequence= {2, 8, 0, 9, 1, 3, 4, 5, 6, 7} 7
55
Depth-First Search55 Example 2 4 3 5 1 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T T T T T T 8 9 -1 1 3 3 5 6 2 8 Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) RDFS(5) no recursive call Recursive calls visit sequence= {2, 8, 0, 9, 1, 3, 4, 5, 6, 7} 7
56
Depth-First Search56 Example 2 4 3 5 1 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T T T T T T 8 9 -1 1 3 3 5 6 2 8 Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) no recursive call Recursive calls visit sequence= {2, 8, 0, 9, 1, 3, 4, 5, 6, 7} 7
57
Depth-First Search57 Example 2 4 3 5 1 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T T T T T T 8 9 -1 1 3 3 5 6 2 8 Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) no recursive call Recursive calls visit sequence= {2, 8, 0, 9, 1, 3, 4, 5, 6, 7} 7
58
Depth-First Search58 Example 2 4 3 5 1 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T T T T T T 8 9 -1 1 3 3 5 6 2 8 Pred RDFS( 2 ) RDFS(8) RDFS(9) no recursive call Recursive calls visit sequence= {2, 8, 0, 9, 1, 3, 4, 5, 6, 7} 7
59
Depth-First Search59 Example 2 4 3 5 1 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T T T T T T 8 9 -1 1 3 3 5 6 2 8 Pred RDFS( 2 ) RDFS(8) no recursive call Recursive calls visit sequence= {2, 8, 0, 9, 1, 3, 4, 5, 6, 7} 7
60
Depth-First Search60 Example 2 4 3 5 1 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T T T T T T 8 9 -1 1 3 3 5 6 2 8 Pred RDFS( 2 ) no recursive call Recursive calls visit sequence= {2, 8, 0, 9, 1, 3, 4, 5, 6, 7} 7
61
Depth-First Search61 Recover a path 2 4 3 5 1 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T T T T T T 8 9 -1 1 3 3 5 6 2 8 Pred visit sequence= {2, 8, 0, 9, 1, 3, 4, 5, 6, 7} 7 Try some examples. Path(0) -> Path(6) -> Path(7) ->
62
Depth-First Search62 DFS Tree The edges that we traverse during DFS (or the edges that we backtrack along) form a tree. We usually call the rooted version (rooted at the source) the DFS tree.
63
63 Minimum Spanning Trees
64
64 Problem: Laying Telephone Wire Central office
65
65 Wiring: Naïve Approach Central office Expensive!
66
66 Wiring: Better Approach Central office Minimize the total length of wire connecting the customers
67
67 Minimum Spanning Tree (MST) (see Weiss, Section 24.2.2) it is a tree (i.e., it is acyclic) it covers all the vertices V contains |V| - 1 edges the total cost associated with tree edges is the minimum among all possible spanning trees not necessarily unique A minimum spanning tree is a subgraph of an undirected weighted graph G, such that
68
Spanning Tree Definition – A spanning tree of a graph G is a tree (acyclic) that connects all the vertices of G once i.e. the tree “spans” every vertex in G – A Minimum Spanning Tree (MST) is a spanning tree on a weighted graph that has the minimum total weight Where might this be useful? Can also be used to approximate some NP-Complete problems
69
Sample MST Which links to make this a MST? Optimal substructure: A subtree of the MST must in turn be a MST of the nodes that it spans. Will use this idea more in dynamic programming.
70
Kruskal’s MST Algorithm Idea: – Go through the list of edges and make a forest that is a MST – At each vertex, sort the edges – Edges with smallest weights examined and possibly added to MST before edges with higher weights – Edges added must be “safe edges” that do not ruin the tree property.
71
Kruskal’s Example A={ }, Make each element its own set. {a} {b} {c} {d} {e} {f} {g} {h} Sort edges. Look at smallest edge first: {c} and {f} not in same set, add it to A, union together. Now get {a} {b} {c f} {d} {e} {g} {h}
72
Kruskal Example Keep going, checking next smallest edge. Had: {a} {b} {c f} {d} {e} {g} {h} {e} <> {h}, add edge. Now get {a} {b} {c f} {d} {e h} {g}
73
Kruskal Example Keep going, checking next smallest edge. Had: {a} {b} {c f} {d} {e h} {g} {a} <> {c f}, add edge. Now get {b} {a c f} {d} {e h} {g}
74
Kruskal’s Example Keep going, checking next smallest edge. Had {b} {a c f} {d} {e h} {g} {b} <> {a c f}, add edge. Now get {a b c f} {d} {e h} {g}
75
Kruskal’s Example Keep going, checking next smallest edge. Had {a b c f} {d} {e h} {g} {a b c f} = {a b c f}, dont add it!
76
Kruskal’s Example Keep going, checking next smallest edge. Had {a b c f} {d} {e h} {g} {a b c f} = {e h}, add it. Now get {a b c f e h} {d}{g}
77
Kruskal’s Example Keep going, checking next smallest edge. Had {a b c f e h} {d}{g} {d} <> {a b c e f h}, add it. Now get {a b c d e f h} {g}
78
Kruskal’s Example Keep going, check next two smallest edges. Had {a b c d e f h} {g} {a b c d e f h} = {a b c d e f h}, don’t add it. 6 5 4 2 9 15 14 10 3 8 a b cd e fg h
79
Kruskal’s Example Do add the last one: Had {a b c d e f h} {g}
80
Kruskal’s Algorithm
81
Prim’s Example
83
Prim’s Algorithm
86
Get spanning tree by connecting nodes with their parents:
87
Prim’s MST Algorithm Will find a MST but may differ from Prim’s if multiple MST’s are possible
88
Shortest Path Algorithms
89
Shortest Path? Example: What is the shortest path from g to b?
90
Shortest Path One way to address this problem is to shift edge values up to remove negative values
91
Relaxation Example
92
Dijkstra Example (0) 6 5 4 2 1 15 14 6 3 8 a b cd e fg h 154
93
Dijkstra Example 1 Extract min, vertex f. S={f}. Update shorter paths. 6 5 4 2 1 15 14 6 3 8 a b cd e fg h 154 INF,NIL 0,NIL INF,NIL
94
Dijkstra Example 2 Extract min, vertex c. S={fc}. Update shorter paths. 6 5 4 2 1 15 14 6 3 8 a b cd e fg h 154 15,f INF,NIL 0,NIL 2,f 4,f 15,f INF,NIL
95
Dijkstra Example 3 Extract min, vertex d. S={fcd}. Update shorter paths (None) 6 5 4 2 1 15 14 6 3 8 a b cd e fg h 154 7,c INF,NIL 0,NIL 2,f 3,c 15,f 6,c
96
Dijkstra Example 4 6 5 4 2 1 15 14 6 3 8 a b cd e fg h 154 7,c INF,NIL 0,NIL 2,f 3,c 15,f Extract min, vertex a. S={fcda}. Update shorter paths (None) Extract min, vertex b. S={fcdab}. Update shorter paths. 6,c
97
Dijkstra Example 5 Extract min, vertex h. S={fcdabh}. Update shorter paths 6 5 4 2 1 15 14 6 3 8 a b cd e fg h 154 7,c INF,NIL 13,b 0,NIL 2,f 3,c 15,f 6,c
98
Dijkstra Example 6 Extract min, vertex g and h – nothing to update, done! 6 5 4 2 1 15 14 6 3 8 a b cd e fg h 154 7,c 16,h 13,b 0,NIL 2,f 3,c 15,f 6,c
99
Dijkstra Example 7 Can follow parent “pointers” to get the path 6 5 4 2 1 15 14 6 3 8 a b cd e fg h 154 7,c 16,h 13,b 0,NIL 2,f 3,c 15,f 6,c
100
Dijkstra’s Algorithm
101
101 Dijkstra’s algorithm S = {1} for i = 2 to n do D[i] = C[1,i] if there is an edge from 1 to i, infinity otherwise for i = 1 to n-1 { choose a vertex w in V-S such that D[w] is min add w to S (where S is the set of visited nodes) for each vertex v in V-S do D[v] = min(D[v], D[w]+c[w,v]) } Where |V| = n
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.