Graph Searching.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Introduction to Algorithms Greedy Algorithms
Types of Algorithms.
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.
Graphs Chapter Chapter Contents Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First.
Chapter 4 The Greedy Method.
Chapter 3 The Greedy Method 3.
1 Greedy Algorithms. 2 2 A short list of categories Algorithm types we will consider include: Simple recursive algorithms Backtracking algorithms Divide.
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.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame.
ASC Program Example Part 3 of Associative Computing Examining the MST code in ASC Primer.
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.
Lecture 23. Greedy Algorithms
The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each decision is locally optimal. These.
Analysis of Algorithms
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Greedy methods Prudence Wong
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Chapter 2 Graph Algorithms.
Minimum Spanning Trees
Dr. Naveed Ahmad Assistant Professor Department of Computer Science University of Peshawar.
알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap4. Greedy Approach Grabs data items in sequence, each time with “best” choice, without.
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
Minimal Spanning Tree Problems in What is a minimal spanning tree An MST is a tree (set of edges) that connects all nodes in a graph, using.
Minimum Spanning Trees CS 146 Prof. Sin-Min Lee Regina Wang.
1 Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: b feasible b locally optimal.
Graphs and MSTs Sections 1.4 and 9.1. Partial-Order Relations Everybody is not related to everybody. Examples? Direct road connections between locations.
Lecture 19 Minimal Spanning Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.
A greedy algorithm is an algorithm that follows the problem solving heuristic of making the locally optimal choice at each stage with the hope of finding.
Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Kruskal’s Algorithm for Computing MSTs Section 9.2.
Greedy Algorithms.
Minimum Spanning Trees
COMP108 Algorithmic Foundations Greedy methods
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
Greedy Technique.
Greedy function greedy { S <- S0 //Initialization
Problem solving sequence
Greedy Algorithms.
Department of Computer Science
Design & Analysis of Algorithm Greedy Algorithm
Spanning Trees Lecture 21 CS2110 – Fall 2016.
Topological Sort (topological order)
Short paths and spanning trees
Data Structures & Algorithms Graphs
Graph Algorithm.
Greedy Method     Greedy Matching     Coin Changing     Minimum Spanning Tree     Fractional Knapsack     Dijkstra's Single-Source Shortest-Path.
CSCE350 Algorithms and Data Structure
Minimum Spanning Tree.
Minimum Spanning Trees
Graphs Chapter 13.
Types of Algorithms.
Graphs.
Advanced Analysis of Algorithms
Minimum Spanning Tree Algorithms
CSCI2100 Data Structures Tutorial
CSC 380: Design and Analysis of Algorithms
Types of Algorithms.
Autumn 2016 Lecture 10 Minimum Spanning Trees
CSC 380: Design and Analysis of Algorithms
Graphs.
Graphs.
Minimum Spanning Trees (MSTs)
The Greedy Approach Young CS 530 Adv. Algo. Greedy.
Graphs.
Spanning Trees Lecture 20 CS2110 – Spring 2015.
Graphs.
Prims’ spanning tree algorithm
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

Graph Searching

The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each decision is locally optimal. These locally optimal solutions will finally add up to a globally optimal solution. Only a few optimization problems can be solved by the greedy method.

Optimization problems An optimization problem is one in which you want to find, not just a solution, but the best solution A “greedy algorithm” sometimes works well for optimization problems A greedy algorithm works in phases. At each phase: You take the best you can get right now, without regard for future consequences You hope that by choosing a local optimum at each step, you will end up at a global optimum 2

Example: Counting money Suppose you want to count out a certain amount of money, using the fewest possible bills and coins A greedy algorithm would do this would be: At each step, take the largest possible bill or coin that does not overshoot Example: To make $6.39, you can choose: a $5 bill a $1 bill, to make $6 a 25¢ coin, to make $6.25 A 10¢ coin, to make $6.35 four 1¢ coins, to make $6.39 For US money, the greedy algorithm always gives the optimum solution 3

A failure of the greedy algorithm In some (fictional) monetary system, “krons” come in 1 kron, 7 kron, and 10 kron coins Using a greedy algorithm to count out 15 krons, you would get A 10 kron piece Five 1 kron pieces, for a total of 15 krons This requires six coins A better solution would be to use two 7 kron pieces and one 1 kron piece This only requires three coins The greedy algorithm results in a solution, but not in an optimal solution 4

A scheduling problem You have to run nine jobs, with running times of 3, 5, 6, 10, 11, 14, 15, 18, and 20 minutes You have three processors on which you can run these jobs You decide to do the longest-running jobs first, on whatever processor is available P1 P2 P3 20 10 3 18 11 6 15 14 5 Time to completion: 18 + 11 + 6 = 35 minutes This solution isn’t bad, but we might be able to do better 5

Another approach What would be the result if you ran the shortest job first? Again, the running times are 3, 5, 6, 10, 11, 14, 15, 18, and 20 minutes P1 P2 P3 3 10 15 5 11 18 6 14 20 That wasn’t such a good idea; time to completion is now 6 + 14 + 20 = 40 minutes Note, however, that the greedy algorithm itself is fast All we had to do at each stage was pick the minimum or maximum 6

An optimum solution Better solutions do exist: 20 18 15 14 11 10 6 5 3 This solution is clearly optimal (why?) Clearly, there are other optimal solutions (why?) How do we find such a solution? One way: Try all possible assignments of jobs to processors Unfortunately, this approach can take exponential time 7

Shortest paths on a special graph Problem: Find a shortest path from v0 to v3. The greedy method can solve this problem. The shortest path: 1 + 2 + 4 = 7.

Shortest paths on a multi-stage graph Problem: Find a shortest path from v0 to v3 in the multi-stage graph. Greedy method: v0v1,2v2,1v3 = 23 Optimal: v0v1,1v2,2v3 = 7 The greedy method does not work.

Minimum spanning trees (MST) It may be defined on Euclidean space points or on a graph. G = (V, E): weighted connected undirected graph Spanning tree : S = (V, T), T  E, undirected tree Minimum spanning tree(MST) : a spanning tree with the smallest total weight.

Minimum Spanning Trees Given: Connected, undirected, weighted graph, G Find: Minimum - weight spanning tree, T Example: 7 b c 5 Acyclic subset of edges(E) that connects all vertices of G. a 1 3 -3 11 d e f 2 b c 5 a 1 3 -3 d e f

An example of MST A graph and one of its minimum costs spanning tree

Kruskal’s algorithm for finding MST Step 1: Sort all edges into nondecreasing order. Step 2: Add the next smallest weight edge to the forest if it will not cause a cycle. Step 3: Stop if n-1 edges. Otherwise, go to Step2.

An example of Kruskal’s algorithm

The single-source shortest path problem shortest paths from v0 to all destinations

Dijkstra’s algorithm In the cost adjacency matrix, all entries not shown are +.

Time complexity : O(n2), n = |V|.

Shortest path from ‘s’ to ‘t’

Questions?