COSC 3101NJ. Elder Assignment 2 Remarking Assignment 2 Marks y = 0.995x + 8.47 R 2 = 0.8558 0 10 20 30 40 50 60 70 80 90 100 050100 Old Mark New Mark.

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.
Advertisements

October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
November 14, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Aalborg University
Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng.
Introduction to Algorithms 6.046J/18.401J/SMA5503
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
Chapter 23 Minimum Spanning Trees
CPSC 411, Fall 2008: Set 9 1 CPSC 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Fall 2008.
CSE 780 Algorithms Advanced Algorithms Shortest path Shortest path tree Relaxation Bellman-Ford Alg.
Lecture 19: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
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.
Shortest Path Problems
1 Data Structures DFS, Topological Sort Dana Shapira.
Lecture 18: Minimum Spanning Trees Shang-Hua Teng.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
TECH Computer Science Graph Optimization Problems and Greedy Algorithms Greedy Algorithms  // Make the best choice now! Optimization Problems  Minimizing.
SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
1 Shortest Path Problems How can we find the shortest route between two points on a road map? Model the problem as a graph problem: –Road map is a weighted.
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1.
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Spring 2015 Lecture 10: Elementary Graph Algorithms
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
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.
Minimum Spanning Trees and Kruskal’s Algorithm CLRS 23.
Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.
Introduction to Algorithms Jiafen Liu Sept
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.
Chapter 23: Minimum Spanning Trees: A graph optimization problem Given undirected graph G(V,E) and a weight function w(u,v) defined on all edges (u,v)
Lecture 13 Algorithm Analysis
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
1 Weighted Graphs. 2 Outline (single-source) shortest path –Dijkstra (Section 4.4) –Bellman-Ford (Section 4.6) (all-pairs) shortest path –Floyd-Warshall.
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.
CSE Graph Search Algorithms. CSE Graph a c b Node ~ city or computer Edge ~ road or data cable Undirected or Directed A surprisingly large.
CSCE 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Spring 2012 CSCE 411, Spring 2012: Set 9 1.
Single Source Shortest Paths Chapter 24 CSc 4520/6520 Fall 2013 Slides adapted from George Bebis, University of Reno, Nevada.
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Graphs – Breadth First Search
Introduction to Algorithms
Algorithms and Data Structures Lecture XIII
Lecture 12 Algorithm Analysis
Graph Search Algorithms
Minimum Spanning Trees
Lecture 10 Algorithm Analysis
Chapter 23 Minimum Spanning Tree
CS 583 Analysis of Algorithms
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Lecture 12 Algorithm Analysis
Lecture 13 Algorithm Analysis
Minimum Spanning Trees
Lecture 13 Algorithm Analysis
Greedy Algorithms Comp 122, Spring 2004.
Lecture 12 Algorithm Analysis
Chapter 23: Minimum Spanning Trees: A graph optimization problem
Minimum Spanning Trees
Presentation transcript:

COSC 3101NJ. Elder Assignment 2 Remarking Assignment 2 Marks y = 0.995x R 2 = Old Mark New Mark

COSC 3101NJ. Elder Section V. Graph Algorithms

COSC 3101NJ. Elder Breadth-First Search Idea: send out search ‘wave’ from s. Keep track of progress by colouring vertices: Undiscovered vertices are coloured white Just discovered vertices (on the wavefront) are coloured grey. Previously discovered vertices (behind wavefront) are coloured black.

COSC 3101NJ. Elder Breadth-First Search Algorithm Each vertex assigned finite d value at most once. d values assigned are monotonically increasing over time. Q contains vertices with d values {i, …, i, i+1, …, i+1} Time = O(V+E)

COSC 3101NJ. Elder Depth-First Search Explore every edge, starting from different vertices if necessary. As soon as vertex discovered, explore from it. Keep track of progress by colouring vertices: White: undiscovered vertices Grey: discovered, but not finished (still exploring from it) Black: finished (found everything reachable from it).

COSC 3101NJ. Elder Depth-First Search Algorithm

COSC 3101NJ. Elder Why is BFS O(S+V), while DFS is  (S+V)? Remember: BFS(G,s) finds the shortest paths from a specified vertex s. If G is either undirected and not connected, or directed and not strongly connected, not all vertices of the graph will be visited in BFS. In contrast, DFS(G) visits all vertices, restarting at multiple source vertices if necessary.

COSC 3101NJ. Elder An Application of DFS: Topological Sorting

COSC 3101NJ. Elder Strongly Connected Components

COSC 3101NJ. Elder Component Graph

COSC 3101NJ. Elder Lemma

COSC 3101NJ. Elder SCC Algorithm

COSC 3101NJ. Elder Lemma

COSC 3101NJ. Elder So why does the SCC Algorithm work?

COSC 3101NJ. Elder Minimum Spanning Trees Example Problem You are planning a new terrestrial telecommunications network to connect a number of remote mountain villages in a developing country. The cost of building a link between pairs of neighbouring villages (u,v) has been estimated: w(u,v). You seek the minimum cost design that ensures each village is connected to the network. The solution is called a minimum spanning tree.

COSC 3101NJ. Elder Building the Minimum Spanning Tree Iteratively construct the set of edges A in the MST. Initialize A to {} As we add edges to A, maintain a Loop Invariant: A is a subset of some MST Maintain loop invariant and make progress by only adding safe edges. An edge (u,v) is called safe for A iff A  {u,v}) is also a subset of some MST.

COSC 3101NJ. Elder Finding a safe edge Idea: Every 2 disjoint subsets of vertices must be connected by at least one edge. Which one should we choose?

COSC 3101NJ. Elder Some definitions A cut (S,V-S) is a partition of vertices into disjoint sets V and S-V. Edge (u,v)  E crosses cut (S, V-S) if one endpoint is in S and the other is in V-S. A cut respects A iff no edge in A cross the cut. An edge is a light edge crossing a cut iff its weight is minimum over all edges crossing the cut.

COSC 3101NJ. Elder Theorem Let A be a subset of some MST (S,V-S) be a cut that respects A (u,v) be a light edge crossing (S,V-S) Then (u,v) is safe for A.

COSC 3101NJ. Elder Proof Let T be an MST that includes A. If T contains (u,v) then we’re done. Suppose T does not contain (u,v) Can construct different MST T' that includes A  {u,v}) The edge (u,v) forms a cycle with the edges on the path from u to v in T. There is at least one edge in T that crosses the cut: let that edge be (x,y) (x,y) is not in A, since the cut (S,V-S) respects A. Form new spanning tree T' by deleting (x,y) from T and adding (u,v). w(T')  w(T), since w(u,v)  w(x,y)  T' is an MST. A  T', since A  T and (x,y)  A  A  {u,v})  T' Thus (u,v) is safe for A. All edges shown (except (u,v)) are in T

COSC 3101NJ. Elder Kruskal’s Algorithm for computing MST Starts with each vertex being its own component. Repeatedly merges two components into one by choosing the light edge that crosses the cut between them. Scans the set of edges in monotonically increasing order by weight. Uses a disjoint-set data structure to determine whether an edge connects vertices in different components.

COSC 3101NJ. Elder Kruskal’s Algorithm for computing MST Running Time = O(ElogE) = O(ElogV)

COSC 3101NJ. Elder Prim’s Algorithm Build one tree Start from arbitrary root r At each step, add light edge crossing cut (V A, V- V A ), where V A = vertices that A is incident on.

COSC 3101NJ. Elder Finding light edges quickly All vertices not in the partial MST formed by A reside in a min- priority queue. Each object is a vertex v in V-V A Key of v is minimum weight of any edge (u,v), u  V A. Each vertex knows its parent in partial MST by  [v].

COSC 3101NJ. Elder Prim’s Algorithm Running Time = O(Elog(V))

COSC 3101NJ. Elder Example: Prim's Algorithm

COSC 3101NJ. Elder Single-Source Shortest Paths

COSC 3101NJ. Elder The Problem What is the shortest driving route from Toronto to Ottawa? (e.g. MAPQuest, Microsoft Streets and Trips) Input: Directed graph G=(V,E) Weight function w: E  R

COSC 3101NJ. Elder Example

COSC 3101NJ. Elder Shortest path variants Single-source shortest-paths problem: – the shortest path from s to each vertex v. (e.g. BFS) Single-destination shortest-paths problem: Find a shortest path to a given destination vertex t from each vertex v. Single-pair shortest-path problem: Find a shortest path from u to v for given vertices u and v. All-pairs shortest-paths problem: Find a shortest path from u to v for every pair of vertices u and v.

COSC 3101NJ. Elder Negative-weight edges OK, as long as no negative-weight cycles are reachable from the source. If we have a negative-weight cycle, we can just keep going around it, and get w(s, v) = −∞ for all v on the cycle. But OK if the negative-weight cycle is not reachable from the source. Some algorithms work only if there are no negative-weight edges in the graph.

COSC 3101NJ. Elder Optimal substructure Lemma: Any subpath of a shortest path is a shortest path Proof: Cut and paste.

COSC 3101NJ. Elder Cycles Shortest paths can’t contain cycles: Already ruled out negative-weight cycles. Positive-weight: we can get a shorter path by omitting the cycle. Zero-weight: no reason to use them  assume that our solutions won’t use them.

COSC 3101NJ. Elder Output of a single-source shortest-path algorithm For each vertex v in V: d[v] = δ(s, v). Initially, d[v]=∞. Reduces as algorithms progress. But always maintain d[v] ≥ δ(s, v). Call d[v] a shortest-path estimate. π[v] = predecessor of v on a shortest path from s. If no predecessor, π[v] = NIL. π induces a tree — shortest-path tree.

COSC 3101NJ. Elder Initialization All shortest-paths algorithms start with the same initialization: INIT-SINGLE-SOURCE(V, s) for each v in V do d[v] ← ∞ π[v] ← NIL d[s] ← 0

COSC 3101NJ. Elder Relaxing an edge Can we improve shortest-path estimate for v by going through u and taking (u,v)? RELAX(u, v,w) if d[v] > d[u] + w(u, v) then d[v] ← d[u] + w(u, v) π[v] ← u

COSC 3101NJ. Elder General single-source shortest-path strategy 1. Start by calling INIT-SINGLE-SOURCE 2. Relax Edges Algorithms differ in the order in which edges are taken and how many times each edge is relaxed.

COSC 3101NJ. Elder Example: Single-source shortest paths in a directed acyclic graph (DAG) Since graph is a DAG, we are guaranteed no negative-weight cycles.

COSC 3101NJ. Elder Algorithm

COSC 3101NJ. Elder Example

COSC 3101NJ. Elder Example

COSC 3101NJ. Elder Example

COSC 3101NJ. Elder Example

COSC 3101NJ. Elder Example

COSC 3101NJ. Elder Example

COSC 3101NJ. Elder Correctness: Path relaxation property (Lemma 24.15)

COSC 3101NJ. Elder Correctness of DAG Shortest Path Algorithm Because we process vertices in topologically sorted order, edges of any path are relaxed in order of appearance in the path.  Edges on any shortest path are relaxed in order.  By path-relaxation property, correct.

COSC 3101NJ. Elder Example: Dijkstra’s algorithm Applies to general weighted directed graph (may contain cycles). But weights must be non-negative. Essentially a weighted version of BFS. Instead of a FIFO queue, uses a priority queue. Keys are shortest-path weights (d[v]). Maintain 2 sets of vertices: S = vertices whose final shortest-path weights are determined. Q = priority queue = V-S.

COSC 3101NJ. Elder Dijkstra’s algorithm Observations: Looks a lot like Prim’s algorithm, but computing d[v], and using shortest-path weights as keys. Dijkstra’s algorithm can be viewed as greedy, since it always chooses the “lightest” (“closest”?) vertex in V − S to add to S.

COSC 3101NJ. Elder Dijkstra’s algorithm: Analysis Analysis: Using binary heap, each operation takes O(logV) time  O(ElogV)

COSC 3101NJ. Elder Example

COSC 3101NJ. Elder Example

COSC 3101NJ. Elder Example

COSC 3101NJ. Elder Example

COSC 3101NJ. Elder Example

COSC 3101NJ. Elder Example

COSC 3101NJ. Elder Correctness of Dijkstra’s algorithm Loop invariant: At the start of each iteration of the while loop, d[v] = δ(s, v) for all v in S. Initialization: Initially, S is empty, so trivially true. Termination: At end, Q is empty  S = V  d[v] = δ(s, v) for all v in V. Maintenance: Need to show that d[u] = δ(s, u) when u is added to S in each iteration. Proof is by contradiction (see text).