Lecture 15. Graph Algorithms

Slides:



Advertisements
Similar presentations
Chapter 23 Minimum Spanning Tree
Advertisements

Great Theoretical Ideas in Computer Science
1 Discrete Structures & Algorithms Graphs and Trees: III EECE 320.
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.
1 Minimum Spanning Tree Prim-Jarnik algorithm Kruskal algorithm.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 12 Graphs.
 Graph Graph  Types of Graphs Types of Graphs  Data Structures to Store Graphs Data Structures to Store Graphs  Graph Definitions Graph Definitions.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
Minimum Spanning Trees Definition Algorithms –Prim –Kruskal Proofs of correctness.
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
Spanning Trees.
Spanning Trees. 2 Spanning trees Suppose you have a connected undirected graph Connected: every node is reachable from every other node Undirected: edges.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Minimum-Cost Spanning Tree weighted connected undirected graph spanning tree cost of spanning tree is sum of edge costs find spanning tree that has minimum.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Lecture 12 Minimum Spanning Tree. Motivating Example: Point to Multipoint Communication Single source, Multiple Destinations Broadcast – All nodes in.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 11 Instructor: Paul Beame.
Design and Analysis of Algorithms Minimum Spanning trees
Spanning Trees. Spanning trees Suppose you have a connected undirected graph –Connected: every node is reachable from every other node –Undirected: edges.
Minimum Spanning Trees. Subgraph A graph G is a subgraph of graph H if –The vertices of G are a subset of the vertices of H, and –The edges of G are a.
Minimum Spanning Trees What is a MST (Minimum Spanning Tree) and how to find it with Prim’s algorithm and Kruskal’s algorithm.
Minimum spanning tree Prof Amir Geva Eitan Netzer.
Data Structures and Algorithms Graphs Minimum Spanning Tree PLSD210.
Minimal Spanning Trees What is a minimal spanning tree (MST) and how to find one.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 2 Graph Algorithms.
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
UNC Chapel Hill Lin/Foskey/Manocha Minimum Spanning Trees Problem: Connect a set of nodes by a network of minimal total length Some applications: –Communication.
Graph Algorithms. Definitions and Representation An undirected graph G is a pair (V,E), where V is a finite set of points called vertices and E is a finite.
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
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.
Union-Find  Application in Kruskal’s Algorithm  Optimizing Union and Find Methods.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Graphs Definition: a graph is an abstract representation of a set of objects where some pairs of the objects are connected by links. The interconnected.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Graph Search Applications, Minimum Spanning Tree
Lecture ? The Algorithms of Kruskal and Prim
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
Minimum Spanning Tree Chapter 13.6.
Lecture 22 Minimum Spanning Tree
Data Structures & Algorithms Graphs
Graph Algorithm.
Minimum-Cost Spanning Tree
Minimum Spanning Tree.
Minimum Spanning Tree.
Minimum Spanning Tree.
Spanning Trees.
Minimum-Cost Spanning Tree
Chapter 11 Graphs.
Minimum Spanning Tree Algorithms
CSCI2100 Data Structures Tutorial
Minimum Spanning Trees
Minimum Spanning Tree.
Graphs G = (V, E) V are the vertices; E are the edges.
CSE 373: Data Structures and Algorithms
Minimum-Cost Spanning Tree
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 .
Minimum Spanning Trees
Presentation transcript:

Lecture 15. Graph Algorithms Talk at U of Maryland Lecture 15. Graph Algorithms An undirected graph G is a pair (V,E), where V is a finite set of points called vertices and E is a finite set of edges. An edge e ∈ E is an unordered pair (u,v), where u,v ∈ V. In a directed graph, the edge e is an ordered pair (u,v). An edge (u,v) is incident from vertex u and is incident to vertex v. A path from a vertex v to a vertex u is a sequence <v0,v1,v2,…,vk> of vertices where v0 = v, vk = u, and (vi, vi+1) ∈ E for i= 0, 1,…, k-1. The length of a path is defined as the number of edges in the path.

a) An undirected graph and (b) a directed graph. Talk at U of Maryland Examples a) An undirected graph and (b) a directed graph.

Talk at U of Maryland More Definitions An undirected graph is connected if every pair of vertices is connected by a path. A forest is an acyclic graph, and a tree is a connected acyclic graph. A graph that has weights associated with each edge is called a weighted graph.

Definitions and Representation Talk at U of Maryland Definitions and Representation Graphs can be represented by their adjacency matrix or an edge (or vertex) list. Adjacency matrices have a value ai,j = 1 if nodes i and j share an edge; 0 otherwise. In case of a weighted graph, ai,j = wi,j, the weight of the edge. The adjacency list representation of a graph G = (V,E) consists of an array Adj[1..|V|] of lists. Each list Adj[v] is a list of all vertices adjacent to v. For a graph with n nodes, adjacency matrices take Θ(n2) space and adjacency list takes Θ(|E|) space.

Definitions and Representation Talk at U of Maryland Definitions and Representation An undirected graph and its adjacency matrix representation. An undirected graph and its adjacency list representation.

Talk at U of Maryland Minimum Spanning Tree A spanning tree of an undirected graph G is a subgraph of G that is a tree containing all the vertices of G. In a weighted graph, the weight of a subgraph is the sum of the weights of the edges in the subgraph. A minimum spanning tree (MST) for a weighted undirected graph is a spanning tree with minimum weight.

An undirected graph and its minimum spanning tree. Talk at U of Maryland Minimum Spanning Tree An undirected graph and its minimum spanning tree.

Algorithms for computing MST Talk at U of Maryland Algorithms for computing MST Prim's algorithm and Kruskal's algorithm. Kruskal's algorithm is a greedy algorithm where, at each step, we choose the minimum-weight edge that doesn't create a cycle. We work with a forest. Initially, each vertex is in a tree by itself. We also work with a set of edges E', which initially holds all the edges of the graph. At each step, we remove and consider a minimum-weight edge from S (break ties arbitrarily). If it connects two different trees, we add it to the forest (joining two trees). Otherwise, it connects two edges in the same tree (forms a cycle), and we discard it. Prim's algorithm is also a greedy algorithm where, at each step, we choose a previously unconnected vertex that becomes connected by a lowest-weight edge. We choose an initial vertex arbitrarily and put it in a set V'. We also have a set E' of edges which is initially empty. At every step, we choose an edge (u,v) of minimal weight with u in V' and v not in V'. We then add v to V' and (u,v) to E'.

Talk at U of Maryland Kruskal’s Algorithm

Time complexity O(|E| log |E|) Talk at U of Maryland Time complexity O(|E| log |E|) Simple implementation of Union-Find gives you log n access time for each operation: Sets will be kept as linked lists of vertices. Each element in the list has a pointer to its next element, and a pointer to the head of the list. There is also a tail pointer to the end of the list, and the list maintains how many elements it contains. The "name" is the first vertex in the list. To do MAKE-SET, we create a linked list of a single element, and the num field set to 1. This costs O(1). To do a FIND, we follow the pointer to the head of the list and return the element there. This costs O(1). To do a UNION(u,v), we follow the pointer of u to its head, and the pointer of v to its head. There we find the number of elements in each list. If (say) u is in the smaller list, we link that list to the end of the bigger list (v's list). Then we traverse u's list, moving each head pointer to point to the head of v's list. Finally, we update the num field of v's list. This costs O(t), where t is the number of vertices of the smaller list. Because we always do the work on the smaller list, each UNION at least doubles the size of the smaller list. So an element's name gets updated at most O(log n) times. Thus n UNIONs take at most O(n log n) time. We will give more details in class.

Talk at U of Maryland Example A A 4 3 3 4 3 3 B 1 2 B 1 2 1 1 7 4 C D C D 3 5 6 8 5 6 4 4 E E F 2 1 F 2 1 Figure on the left shows a graph with sorted order (inside text boxes) written beside the edges plus the cost corresponding to each edge. Starting from A we can see A to C has order one so this edge will be added first to spanning tree Second in order is an edge between E and F so we add it. Next we add edge between A and D and one edge between A and B. Edge between C and D is discarded as it creates a cycle Next an edge between D and F is added even if D and F have been explored. Edge between B and C is dropped and edge between C and E is dropped

Minimum Spanning Tree: Prim's Algorithm Talk at U of Maryland Minimum Spanning Tree: Prim's Algorithm

Time complexity O(|E| + |V| log |V|). Talk at U of Maryland Time complexity O(|E| + |V| log |V|). Using heap, gives you O(|E| log |V|). Since after decrease value, we need to re-do the heap. But this can be improved using Fibonacci heap (not covered here).

Prim's minimum spanning tree algorithm. Talk at U of Maryland Prim's Algorithm Example Prim's minimum spanning tree algorithm.