Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

Similar documents
04/03/03 Lecture 20 1

Today s Outline. Graph ADT? Graph Definitions. Trees as Graphs. Graphs Chapter 9 in Weiss. More Definitions: Simple Paths and Cycles.

CSE 100 Minimum Spanning Trees Prim s and Kruskal

COP 3530, Course Outcomes

Algorithm Analysis Graph algorithm. Chung-Ang University, Jaesung Lee

Minimum Spanning Trees

Basic Graph Definitions

ECE4050 Data Structures and Algorithms. Lecture 8: Graphs

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

Week 12: Minimum Spanning trees and Shortest Paths

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

6.1 Minimum Spanning Trees

Elementary Graph Algorithms: Summary. Algorithms. CmSc250 Intro to Algorithms

Lecture 10. Graphs Vertices, edges, paths, cycles Sparse and dense graphs Representations: adjacency matrices and adjacency lists Implementation notes

Minimum Spanning Trees

Minimum Spanning Trees. Minimum Spanning Trees. Minimum Spanning Trees. Minimum Spanning Trees

CS61B Lecture #35. Today: A* search, Minimum spanning trees, union-find. Last modified: Fri Nov 25 20:16: CS61B: Lecture #35 1

Mystery Algorithm! ALGORITHM MYSTERY( G = (V,E), start_v ) mark all vertices in V as unvisited mystery( start_v )

SPANNING TREES. Lecture 21 CS2110 Spring 2016

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 8 Lecturer: David Wagner February 20, Notes 8 for CS 170

CS 146 Spring 2017 Homework 4 Sections 5 and 6. The homework assignment is available at:

Graphs. 04/01/03 Lecture 20 1

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Data Structures and Algorithms

Info 2950, Lecture 16

Priority queue ADT part 1

Unit #9: Graphs. CPSC 221: Algorithms and Data Structures. Will Evans 2012W1

CS61B, Fall 2002 Discussion #15 Amir Kamil UC Berkeley 12/5/02

Week 11: Minimum Spanning trees

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

CS 161: Design and Analysis of Algorithms

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

Points off Total off Net Score. CS 314 Final Exam Spring 2016

CS/COE 1501 cs.pitt.edu/~bill/1501/ Network Flow

(Dijkstra s Algorithm) Consider the following positively weighted undirected graph for the problems below: 8 7 b c d h g f

CSE 373: Practice Final

Greedy Algorithms. COMP 215 Lecture 6

Algorithm Design, Anal. & Imp., Homework 4 Solution

CS 310 Advanced Data Structures and Algorithms

Homework Assignment #3 Graph

2.1 Greedy Algorithms. 2.2 Minimum Spanning Trees. CS125 Lecture 2 Fall 2016

Graph Applications. Topological Sort Shortest Path Problems Spanning Trees. Data Structures 1 Graph Applications

CSE 431/531: Analysis of Algorithms. Greedy Algorithms. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

CSE 100: GRAPH ALGORITHMS

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li

CS Algorithms and Complexity

We ve done. Introduction to the greedy method Activity selection problem How to prove that a greedy algorithm works Fractional Knapsack Huffman coding

Graph G = (V, E) V = set of vertices (nodes) E = set of edges (arcs)(lines) (arrows) D F E A C

Algorithms and Data Structures (INF1) Lecture 15/15 Hua Lu

Lecture 18: Implementing Graphs

CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013

Prim & Kruskal Algorithm

Minimum Spanning Trees My T. UF

Tree Construction (BFS, DFS, MST) Chapter 5. Breadth-First Spanning Tree (BFS)

CSE 100: GRAPH ALGORITHMS

Outline. Graphs. Divide and Conquer.

4/8/11. Single-Source Shortest Path. Shortest Paths. Shortest Paths. Chapter 24

Lecture 20: Other Algorithms on Graphs. Undirected Trees. Spanning Trees

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

CSE 100: GRAPH SEARCH

CSC 8301 Design & Analysis of Algorithms: Kruskal s and Dijkstra s Algorithms

Minimum Spanning Trees

Lecture 19. Broadcast routing

Massive Data Algorithmics. Lecture 10: Connected Components and MST

COS 226 Lecture 19: Minimal Spanning Trees (MSTs) PFS BFS and DFS examples. Classic algorithms for solving a natural problem

CS 61B Data Structures and Programming Methodology. Aug 5, 2008 David Sun

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

Problem Score Maximum MC 34 (25/17) = 50 Total 100

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

Prelim 2 Solutions. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Minimum Spanning Trees

CS4800: Algorithms & Data Jonathan Ullman

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

ECE 242 Data Structures and Algorithms. Graphs II. Lecture 27. Prof.

Chapter 9 Graph Algorithms

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

Data Structures and Algorithms. Werner Nutt

Minimum Spanning Trees

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

Lecture 7 Data Structures (DAT037) Ramona Enache (with slides from Nick Smallbone and Nils Anders Danielsson)

Announcements Problem Set 5 is out (today)!

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs

Graph Algorithms (part 3 of CSC 282),

Minimum spanning trees

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018

Quiz 2 CS 3510 October 22, 2004

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

ROOT: A node which doesn't have a parent. In the above tree. The Root is A.

CSE332: Data Abstractions Lecture 25: Minimum Spanning Trees. Ruth Anderson via Conrad Nied Winter 2015

Unweighted Graphs & Algorithms

15 211: RECITATION7, SECTION M. Hui Han Chin,

Data Structures and Algorithms. Chapter 7. Graphs

ECE250: Algorithms and Data Structures Elementary Graph Algorithms Part A

tree follows. Game Trees

Transcription:

Data Structures Giri Narasimhan Office: ECS 254A Phone: x-3748 giri@cs.fiu.edu

Lecture 18 03/11/04

Vertex and Edge classes Class Edge { public Vertex dest; public double weight; public Edge (Vertex d, double w) { dest = d; weight = w; Class Vertex { public String Name; public AnyType extrainfo; public List adj; public int dist; // double? public Vertex prev; public Vertex (String s) { Name = s; adj = new LinkedList(); reset(); public reset () { dist=infnt; path=null;

Topological Sort void topsort () { for( int j = 0; j < N; j++) { Vertex v = findvertexofindegzero(); if (v == null) return; // Cycle found v.topologicalnum = j; for each vertex w adjacent to v w.indegree--; // use extrainfo field Time Complexity = O(n + m)

Unweighted SP algorithm Void BFS (Vertex s) { // same as unweighted SP Queue <Vertex> Q = new Queue <>; for each Vertex v except s { v.dist = INFNT; s.dist = 0; s.prev = null; Q.enqueue(s); while (!Q.isEmpty() ) { v = Q.dequeue(); for each vertex w adjacent to v if (w.dist == INFNT) { w.dist = v.dist + 1; w.prev = v; Q.enqueue(w); Time Complexity = O(n + m)

Dijkstra s SP algorithm void Dijkstra (Vertex s) { // same as weighted SP PriorityQueue <Vertex> Q = new PriorityQueue <>; for each Vertex v except s { v.dist = INFNT; Q.insert(v); s.dist = 0; s.prev= null; Q.insert(s); while (!Q.isEmpty() ) { v = Q.deleteMin(); for each vertex w adjacent to v if (w.dist > v.dist + weight of edge (v,w)) { w.dist = v.dist + weight of edge (v,w); w.prev= v; Q.updatePriority(w, v.dist + weight of edge (v,w)); Time Complexity = O(n log n + m + m log n) = O(m log n)

Spanning Trees and MST u Graph G(V,E) u Path: sequence of edges u Cycle: closed path u A subgraph G of G is given by G (V, E ) u A tree is a graph with no cycles u A connected graph has a path between all vertices u A spanning tree is a tree that connects all vertices

Minimum Spanning Trees u Input: Weighted Graph, G(V,E) with n vertices & m edges, and edge weights w(e) for edge e u Output: Find a spanning tree of minimum total weight

Observations and Facts u Every spanning tree has n-1 edges. u A spanning tree connects all vertices of a graph. u A spanning tree has no cycles. u If there is a cycle in the original graph, any spanning tree must miss at least one edge. q MST misses one heaviest edge on that cycle. u Given any partition of vertices into (S, V-S), every spanning tree must include at least one edge from S to V-S. q MST includes one lightest edge from E(S,V-S).

MST Prim s Algorithm

Prim s Algorithm

Reminder of Dijkstra s Alg void Dijkstra (Vertex s) { // same as weighted SP PriorityQueue <Vertex> Q = new PriorityQueue <>; for each Vertex v except s { v.dist = INFNT; Q.insert(v); s.dist = 0; s.prev= null; Q.insert(s); while (!Q.isEmpty() ) { v = Q.deleteMin(); for each vertex w adjacent to v if (w.dist > v.dist + weight of edge (v,w)) { w.dist = v.dist + weight of edge (v,w); w.prev= v; Q.updatePriority(w, v.dist + weight of edge (v,w)); Time Complexity = O(n log n + m + m log n) = O(m log n)

Prim s Algorithm ArrayList <Edges> Prim (Vertex s) { // computes MST PriorityQueue <Vertex> Q = new PriorityQueue <>; for each Vertex v except s { v.dist = INFNT; Q.insert(v); s.dist = 0; s.prev= null; Q.insert(s); while (!Q.isEmpty() ) { v = Q.deleteMin(); for each vertex w adjacent to v if (w.dist > weight of edge (v,w)) { w.dist = weight of edge (v,w); w.prev= v; Q.updatePriority(w, weight of edge (v,w)); for each Vertex v except s { compile List mst with edges (v.prev, v); return mst; Time Complexity = O(n log n + m + m log n) = O(m log n)

Kruskal s Algorithm

Pseudocode: Kruskal s ArrayList <Edges> Kruskal( List <Edge> edges, int N) { PriorityQueue<Edge> Q = new PriorityQueue<>(edges); List<Edge> mst = new ArrayList<>(); while mst.size()!= N-1) { Edge e = Q.deleteMin(); if (EdgeConnectsDifferentComponents(e)) { mst.add(e); UpdateComponents(e); return mst; Time Complexity = O(m log m) = O(m log n)