CPSC 320: Intermediate Algorithm Design & Analysis Greedy Algorithms and Graphs Steve Wolfman 1.

Slides:



Advertisements
Similar presentations
CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2011W2.
Advertisements

Lecture 15. Graph Algorithms
Chapter 23 Minimum Spanning Tree
Weighted graphs Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities.
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.
Chapter 4 The Greedy Approach. Minimum Spanning Tree A tree is an acyclic, connected, undirected graph. A spanning tree for a given graph G=, where E.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
1 CSE 326: Data Structures: Graphs Lecture 19: Monday, Feb 24, 2003.
 Graph Graph  Types of Graphs Types of Graphs  Data Structures to Store Graphs Data Structures to Store Graphs  Graph Definitions Graph Definitions.
Lecture 3: Greedy Method Greedy Matching Coin Changing Minimum Spanning Tree Fractional Knapsack Dijkstra's Single-Source Shortest-Path.
Algorithm Strategies Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
ITEC200 – Week 12 Graphs. 2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study.
1 Spanning Trees Lecture 20 CS2110 – Spring
Chapter 9 Graph algorithms. Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
CSE 326: Data Structures Lecture #19 Graphs I Alon Halevy Spring Quarter 2001.
CSE 326 Graphs David Kaplan Dept of Computer Science & Engineering Autumn 2001.
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.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
CSE 326: Data Structures Graphs Ben Lerner Summer 2007.
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.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Graphs & Graph Algorithms 2 Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
CSE 326: Data Structures Lecture #17 Trees and DAGs and Graphs, Oh MY! Bart Niswonger Summer Quarter 2001.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Graphs & Graph Algorithms 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
Analysis of Algorithms
CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
SPANNING TREES Lecture 21 CS2110 – Spring
CSE332: Data Structures & Algorithms Lecture 13: Introduction to Graphs Dan Grossman Fall 2013.
Spring 2015 Lecture 11: Minimum Spanning Trees
Dr. Naveed Ahmad Assistant Professor Department of Computer Science University of Peshawar.
Prims’ spanning tree algorithm Given: connected graph (V, E) (sets of vertices and edges) V1= {an arbitrary node of V}; E1= {}; //inv: (V1, E1) is a tree,
CSE 326: Data Structures First Post-Schism Lecture Lecture #22 SteganoGRAPHy Steve Wolfman Winter Quarter 2000.
CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001.
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, Based on recitation.
CSE332: Data Abstractions Lecture 15: Introduction to Graphs Tyler Robison 2010 Summer 1.
Minimum Spanning Trees CS 146 Prof. Sin-Min Lee Regina Wang.
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
SPANNING TREES Lecture 21 CS2110 – Fall Nate Foster is out of town. NO 3-4pm office hours today!
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
Lecture 20. Graphs and network models 1. Recap Binary search tree is a special binary tree which is designed to make the search of elements or keys in.
Graphs. What is a graph? In simple words, A graph is a set of vertices and edges which connect them. A node (or vertex) is a discrete position in the.
SPANNING TREES Lecture 21 CS2110 – Spring
1 CSE 332: Graphs Richard Anderson Spring Announcements This week and next week – Graph Algorithms Reading, Monday and Wednesday, Weiss
CSE 326: Data Structures Lecture #23 Dijkstra and Kruskal (sittin’ in a graph) Steve Wolfman Winter Quarter 2000.
Chapter 5. Greedy Algorithms
Minimum Spanning Tree Chapter 13.6.
CSE373: Data Structures & Algorithms Lecture 16: Introduction to Graphs Linda Shapiro Winter 2015.
Spanning Trees Lecture 21 CS2110 – Fall 2016.
CS120 Graphs.
CSE373: Data Structures & Algorithms Lecture 16: Introduction to Graphs Linda Shapiro Spring 2016.
Greedy Method     Greedy Matching     Coin Changing     Minimum Spanning Tree     Fractional Knapsack     Dijkstra's Single-Source Shortest-Path.
Graphs & Graph Algorithms 2
Discrete Mathematics CMP-101 Lecture 12 Sorting, Bubble Sort, Insertion Sort, Greedy Algorithms Abdul Hameed
Graph Searching.
Spanning Trees Lecture 20 CS2110 – Spring 2015.
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

CPSC 320: Intermediate Algorithm Design & Analysis Greedy Algorithms and Graphs Steve Wolfman 1

Problem-Solving Approaches Many problems can be solved by the same broad style of approach. We’ll run into several of these styles: – Input consuming (like insertion sort) – Output producing (like selection sort) – Divide-and-Conquer (like merge sort) – Greedy (like change-making) – Dynamic Programming 2

Optimization Problems In an optimization problem, we want to report a * best answer according to some metric (i.e., a function that maps correct solutions to their value, where lower values are better). Example: coin changing. Given an amount of change to make, give that amount of change with the fewest coins. 3 * Why do we say “a best answer” and not “the best answer”?

Greedy (and not) Coin Changing Coin changing with Cdn coins: penny, nickel, dime, quarter – Repeatedly add the largest coin that “fits” in the change remaining to be made. Coin changing with Cdn coins but no nickels: penny, dime, quarter – Does the algorithm above work? (Try $0.30.) Coin changing with Cdn coins plus the bauxel: penny, nickel, dime, bauxel ($0.15), quarter – Is there only one best solution? (Try $0.30.) 4

Greedy Algorithms Repeatedly make the “locally best choice” until the choices form a complete solution. 5

More Greedy (or not) Problems Activity Selection Minimum Spanning Tree Shortest Path 6 The latter two are graph problems; so, some graph review is in order...

Interesting Properties for Greedy Algorithms Optimal substructure: An optimal solution to the problem is composed of pieces which are themselves optimal solutions to subproblems. Greedy-choice property: locally optimal (greedy) choices can be extended to a globally optimal solution. 7

Graph ADT Graphs are a formalism useful for representing relationships between things –a graph G is represented as G = (V, E) V is a set of vertices: {v 1, v 2, …, v n } E is a set of edges: {e 1, e 2, …, e m } where each e i connects two vertices (v i1, v i2 ) –operations might include: creation (with a certain number of vertices) inserting/removing edges iterating over vertices adjacent to a specific vertex asking whether an edge exists connecting two vertices Han Leia Luke V = {Han, Leia, Luke} E = {(Luke, Leia), (Han, Leia), (Leia, Han)} 8

Graph Applications Storing things that are graphs by nature –distance between cities –airline flights, travel options –relationships between people, things –distances between rooms in Clue Compilers –callgraph - which functions call which others –dependence graphs - which variables are defined and used at which statements Others: mazes, circuits, class hierarchies, horses, networks of computers or highways or… 9

Graph Representations 2-D matrix of vertices (marking edges in the cells) “adjacency matrix” List of vertices each with a list of adjacent vertices “adjacency list” Han Leia Luke 10

Adjacency Matrix A |V| x |V| array in which an element (u, v) is true if and only if there is an edge from u to v Han Leia Luke HanLukeLeia Han Luke Leia runtime for various operations?space requirements: 11

Adjacency List A |V| -ary list (array) in which each entry stores a list (linked list) of all adjacent vertices Han Leia Luke Han Luke Leia space requirements: 12 runtime for various operations?

Directed vs. Undirected Graphs Adjacency lists and matrices both work fine to represent directed graphs. To represent undirected graphs, either ensure that both orderings of every edge are included in the representation or ensure that the order doesn’t matter (e.g., always use a “canonical” order), which works poorly in adjacency lists. 13

Weighted Graphs Mukilteo Edmonds Seattle Bremerton Bainbridge Kingston Clinton As far as we’re concerned, weight is a function from the set of nodes to the reals. Each edge has an associated weight or cost. 14

Unweighted Shortest Path Problem Assume source vertex is C … A C B D FH G E Distance to: A B C D E F G H

Weighted Shortest Path A C B D FH G E

Spanning tree: a subset of the edges from a connected graph that… …touches all vertices in the graph (spans the graph) …forms a tree (is connected and contains no cycles) Minimum spanning tree: the spanning tree with the least total edge cost. Spanning Tree

Prim’s Algorithm Sample Graph A C B D FH G E

Kruskal’s Algorithm Sample Graph A C B D FH G E

What’s Next? Dynamic Programming: CLRS Chapter 15 20

On Your Own Review graphs Practice designing and analyzing greedy algorithms for optimality/performance. Play around with problems to see when small changes can keep a greedy algorithm from working (like dropping nickels from coin changing). 21