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.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Greedy Algorithms Greed is good. (Some of the time)
Greed is good. (Some of the time)
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
More Graph Algorithms Minimum Spanning Trees, Shortest Path Algorithms.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Graph & BFS.
Topological Sort Introduction. Definition of Topological Sort.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Graphs. Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
COMP171 Depth-First Search.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Depth-first search COMP171 Fall Graph / Slide 2 Depth-First Search (DFS) * DFS is another popular graph search strategy n Idea is similar to pre-order.
1 Topological Sort Introduction. Definition of Topological Sort. Topological Sort is Not Unique. Topological Sort Algorithms. An Example. Implementation.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Depth-First Search Lecture 24 COMP171 Fall Graph / Slide 2 Depth-First Search (DFS) * DFS is another popular graph search strategy n Idea is similar.
Topological Sort Introduction. Definition of Topological Sort. Topological Sort is Not Unique. Topological Sort Algorithm. An Example. Implementation.
1 Minimum Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
1 Minimum Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
B-Tree. B-Trees a specialized multi-way tree designed especially for use on disk In a B-tree each node may contain a large number of keys. The number.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 2 Graph Algorithms.
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
COSC 2007 Data Structures II Chapter 14 Graphs III.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
 What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree.
Graph Traversal BFS & DFS. Review of tree traversal methods Pre-order traversal In-order traversal Post-order traversal Level traversal a bc d e f g hi.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
1 Minimum Spanning Trees (some material adapted from slides by Peter Lee, Ananda Guna, Bettina Speckmann)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graphs Upon completion you will be able to:
Graphs and Paths : Chapter 15 Saurav Karmakar
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University Graphs Original Slides by Rada Mihalcea.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Chapter 5 : Trees.
Introduction to Graphs
Depth-First Search.
CS202 - Fundamental Structures of Computer Science II
Graph Traversals Depth-First Traversals. Algorithms. Example.
Topological Sort Introduction. Definition of Topological Sort.
Graphs Chapter 11 Objectives Upon completion you will be able to:
CSE 373 Data Structures and Algorithms
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Minimum Spanning Tree Section 7.3: Examples {1,2,3,4}
Chapter 11 Graphs.
B-TREE ________________________________________________________
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Minimum Spanning Trees
Minimum Spanning Tree.
Topological Sort Introduction. Definition of Topological Sort.
Topological Sort Introduction. Definition of Topological Sort.
Topological Sort Introduction. Definition of Topological Sort.
GRAPHS.
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

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.

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:

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:

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.

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:

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.

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

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.)

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.

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.

Topological Sort Introduction. Definition of Topological Sort. Topological Sort is Not Unique. Topological Sort Algorithm. An Example. Implementation. Review Questions.

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.

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)

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.

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; }

Topological Sort Example Demonstrating Topological Sort. A F B G C H D I E J D G A B F H J E I C

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)]++; }

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; }

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.

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)}

Applications electronic circuits networks (roads, flights, communications) CS16 LAX JFK LAX DFW STL HNL FTL

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

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

G1G1 G2G directed graph in-degree out-degree G3G3 in:1, out: 1 in: 1, out: 2 in: 1, out: Examples

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 a b c d e a b c d e a b e d cb e d c

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

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

(i) (ii) (iii) (iv) (a) Some of the subgraph of G (i) (ii) (iii) (iv) (b) Some of the subgraph of G G1G G3G3 Subgraphs Examples

More… tree - connected graph without cycles forest - collection of trees

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

Graph Representations Adjacency Matrix Adjacency Lists

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

Examples for Adjacency Matrix G1G1 G2G2 G4G symmetric undirected: n 2 /2 directed: n 2

G1G G3G G4G An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes

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.

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).

Depth-First Search40 Example Adjacency List source Visited Table (T/F) F F F F F F F F F F Initialize visited table (all empty F) Initialize Pred to Pred

Depth-First Search41 Example Adjacency List source Visited Table (T/F) F F T F F F F F F F Mark 2 as visited Pred RDFS( 2 ) recursive call  RDFS(8) visit sequence= {2}

Depth-First Search42 Example Adjacency List source Visited Table (T/F) F F T F F F F F T F Mark 8 as visited Pred RDFS( 2 ) RDFS(8) recursive call  RDFS(0) Recursive calls visit sequence= {2, 8}

Depth-First Search43 Example Adjacency List source Visited Table (T/F) T F T F F F F F T F Mark 0 as visited Pred RDFS( 2 ) RDFS(8) RDFS(0) -> no unvisited neighbor, return to (backtrack) RDFS(8) Recursive calls visit sequence= {2, 8, 0}

Depth-First Search44 Example Adjacency List source Visited Table (T/F) T F T F F F F F T F Pred RDFS( 2 ) RDFS(8) recursive call  RDFS(9) Recursive calls Backtrack to 8 visit sequence= {2, 8, 0}

Depth-First Search45 Example Adjacency List source Visited Table (T/F) T F T F F F F F T T Mark 9 as visited Pred RDFS( 2 ) RDFS(8) RDFS(9) recursive call  RDFS(1) Recursive calls visit sequence= {2, 8, 0, 9}

Depth-First Search46 Example Adjacency List source Visited Table (T/F) T T T F F F F F T T Mark 1 as visited Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) recursive call  RDFS(3) Recursive calls visit sequence= {2, 8, 0, 9, 1}

Depth-First Search47 Example Adjacency List source Visited Table (T/F) T T T T F F F F T T Mark 3 as visited 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}

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 Adjacency List source Visited Table (T/F) T T T T T F F F T T Mark 4 as visited Pred Recursive calls visit sequence= {2, 8, 0, 9, 1, 3, 4}

Depth-First Search49 Example Adjacency List source Visited Table (T/F) T T T T T F F F T T 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}

Depth-First Search50 Example Adjacency List source Visited Table (T/F) T T T T T T F F T T 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}

Depth-First Search51 Example Adjacency List source Visited Table (T/F) T T T T T T T F T T 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}

Depth-First Search52 Example Adjacency List source Visited Table (T/F) T T T T T T T T T T 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

Depth-First Search53 Example Adjacency List source Visited Table (T/F) T T T T T T T T T T 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

Depth-First Search54 Example Adjacency List source Visited Table (T/F) T T T T T T T T T T 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

Depth-First Search55 Example Adjacency List source Visited Table (T/F) T T T T T T T T T T 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

Depth-First Search56 Example Adjacency List source Visited Table (T/F) T T T T T T T T T T 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

Depth-First Search57 Example Adjacency List source Visited Table (T/F) T T T T T T T T T T 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

Depth-First Search58 Example Adjacency List source Visited Table (T/F) T T T T T T T T T T 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

Depth-First Search59 Example Adjacency List source Visited Table (T/F) T T T T T T T T T T Pred RDFS( 2 ) RDFS(8)  no recursive call Recursive calls visit sequence= {2, 8, 0, 9, 1, 3, 4, 5, 6, 7} 7

Depth-First Search60 Example Adjacency List source Visited Table (T/F) T T T T T T T T T T Pred RDFS( 2 )  no recursive call Recursive calls visit sequence= {2, 8, 0, 9, 1, 3, 4, 5, 6, 7} 7

Depth-First Search61 Recover a path Adjacency List source Visited Table (T/F) T T T T T T T T T T Pred visit sequence= {2, 8, 0, 9, 1, 3, 4, 5, 6, 7} 7 Try some examples. Path(0) -> Path(6) -> Path(7) ->

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 Minimum Spanning Trees

64 Problem: Laying Telephone Wire Central office

65 Wiring: Naïve Approach Central office Expensive!

66 Wiring: Better Approach Central office Minimize the total length of wire connecting the customers

67 Minimum Spanning Tree (MST) (see Weiss, Section )  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

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

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.

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.

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}

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}

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}

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}

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!

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}

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}

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 a b cd e fg h

Kruskal’s Example Do add the last one: Had {a b c d e f h} {g}

Kruskal’s Algorithm

Prim’s Example

Prim’s Algorithm

Get spanning tree by connecting nodes with their parents:

Prim’s MST Algorithm Will find a MST but may differ from Prim’s if multiple MST’s are possible

Shortest Path Algorithms

Shortest Path? Example: What is the shortest path from g to b?

Shortest Path One way to address this problem is to shift edge values up to remove negative values

Relaxation Example

Dijkstra Example (0) a b cd e fg h 154

Dijkstra Example 1 Extract min, vertex f. S={f}. Update shorter paths a b cd e fg h 154 INF,NIL 0,NIL INF,NIL

Dijkstra Example 2 Extract min, vertex c. S={fc}. Update shorter paths a b cd e fg h ,f INF,NIL 0,NIL 2,f 4,f 15,f INF,NIL

Dijkstra Example 3 Extract min, vertex d. S={fcd}. Update shorter paths (None) a b cd e fg h 154 7,c INF,NIL 0,NIL 2,f 3,c 15,f 6,c

Dijkstra Example 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

Dijkstra Example 5 Extract min, vertex h. S={fcdabh}. Update shorter paths a b cd e fg h 154 7,c INF,NIL 13,b 0,NIL 2,f 3,c 15,f 6,c

Dijkstra Example 6 Extract min, vertex g and h – nothing to update, done! a b cd e fg h 154 7,c 16,h 13,b 0,NIL 2,f 3,c 15,f 6,c

Dijkstra Example 7 Can follow parent “pointers” to get the path a b cd e fg h 154 7,c 16,h 13,b 0,NIL 2,f 3,c 15,f 6,c

Dijkstra’s Algorithm

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