Discussion section #2 HW1 questions?

Slides:



Advertisements
Similar presentations
Bellman-Ford algorithm
Advertisements

Single Source Shortest Paths
* Bellman-Ford: single-source shortest distance * O(VE) for graphs with negative edges * Detects negative weight cycles * Floyd-Warshall: All pairs shortest.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 CHAPTER 4 - PART 2 GRAPHS 1.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Breadth First Search (BFS) Part 2 COMP171. Graph / Slide 2 Shortest Path Recording * BFS we saw only tells us whether a path exists from source s, to.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path 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.
COMP261 Lecture 6 Dijkstra’s Algorithm. Connectedness Is this graph connected or not? A Z FF C M N B Y BB S P DDGG AA R F G J L EE CC Q O V D T H W E.
CS 61B Data Structures and Programming Methodology Aug 5, 2008 David Sun.
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.
TIRGUL 10 Dijkstra’s algorithm Bellman-Ford Algorithm 1.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
Graphs – Part III CS 367 – Introduction to Data Structures.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
Graph Search Applications, Minimum Spanning Tree
Shortest Paths.
Graphs Representation, BFS, DFS
COMP108 Algorithmic Foundations Greedy methods
Shortest Paths and Minimum Spanning Trees
Lecture 13 Shortest Path.
CSC317 Shortest path algorithms
Single-Source Shortest Path
CSE 2331/5331 Topic 9: Basic Graph Alg.
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Topological Sort (topological order)
CS330 Discussion 6.
More Graph Algorithms.
Discussion section #2 HW1 questions?
Shortest Paths with Dynamic Programming
Graphs Representation, BFS, DFS
Data Structures and Algorithms
Unit-5 Dynamic Programming
Shortest Paths.
CSCE 411 Design and Analysis of Algorithms
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Search Related Algorithms
Unit 4: Dynamic Programming
CSE 373: Data Structures and Algorithms
Outline This topic covers Prim’s algorithm:
Chapter 11 Graphs.
Graphs Part 2 Adjacency Matrix
Depth-First Search Graph Traversals Depth-First Search DFS.
Shortest Path Algorithms
Shortest Paths and Minimum Spanning Trees
Floyd’s Algorithm (shortest-path problem)
CSCE 411 Design and Analysis of Algorithms
Dynamic Programming Longest Path in a DAG, Yin Tat Lee
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
COMP171 Depth-First Search.
CSE 373: Data Structures and Algorithms
Depth-First Search CSE 2011 Winter April 2019.
CSE 373: Data Structures and Algorithms
Shortest Path Algorithm for Weighted Non-negative Undirected Graphs
Shortest Paths.
Depth-First Search CSE 2011 Winter April 2019.
Shortest Paths.
CSE 417: Algorithms and Computational Complexity
Dijkstra's Shortest Path Algorithm
CE 221 Data Structures and Algorithms
Algorithms CSCI 235, Spring 2019 Lecture 35 Graphs IV
Applications of BFS CSE 2011 Winter /17/2019 7:03 AM.
Lecture 12 Shortest Path.
CSE 373: Data Structures and Algorithms
Chapter 24: Single-Source Shortest-Path
Lecture 36 CSE 331 Nov 22, 2013.
Presentation transcript:

Discussion section #2 HW1 questions? HW2: highest-weighed path on a DAG Shortest-path finding algorithms Memoization

HW2: highest-weighted path on a DAG

HW2: highest-weight path on a DAG Create input file with one line for each vertex and edge Find highest-weight path on the graph Output: Path length Beginning and end vertex labels The labels of all the edges on the path, in order For a given DAG and a genomic sequence

HW2: highest-weight path on a DAG Create input file with one line for each vertex and edge Find highest-weight path on the graph Output: Path length Beginning and end vertex labels The labels of all the edges on the path, in order How to store the graph after reading it in?

HW2: highest-weight path on a DAG

Shortest path algorithms!

Shortest path on DAG Pretty much same algorithm as homework Just look for minimum instead of maximum Have to choose specific source and/or destination (because shortest path overall is always 0)

What if graph has cycles? More difficult (can’t order nodes by depth) Bellman-Ford algorithm Choose source node and set distance to 0 Set distance to all other nodes to infinity For each edge u->v, if v’s distance can be reduced by taking that edge, update v’s distance Cycle through all edges in this way |V|-1 times Repeat for all vertices as source node (can also check for negative-weight cycle with one extra iteration)

What if graph has no negative edges? Less difficult (we can visit some nodes fewer times) Djikstra’s algorithm Choose source node and set distance to 0 Set distance to all other nodes to infinity Set source node to current Make distance offers to all unvisited neighbors, which are accepted if they’re less than the previous best offer Mark current as visited (it will never be updated again) Select unvisited neighbor with smallest distance, set it to current, and repeat (When destination node has been marked visited, stop)

Two approaches to dynamic programming Tabulation (“dynamic programming”) Memoization What we’ve been doing Bottom-up approach Lazier (?) Top-down approach

Two approaches to dynamic programming Tabulation (“dynamic programming”) Memoization What we’ve been doing Bottom-up approach Lazier (?) Top-down approach function fib(n) fib = [0, 1] for i in 2..n fib[i] = fib[i-1] + fib[i-2] return fib[n] function fib(n, solutions) if n in solutions return solutions[n] if n == 0 return 0 if n == 1 return 1 current = fib[n-1] + fib[n-2] solutions[n] = current return current

Two approaches to dynamic programming Tabulation (“dynamic programming”) Memoization What we’ve been doing Bottom-up approach Lazier (?) Top-down approach function fib(n) fib = [0, 1] for i in 2..n fib[i] = fib[i-1] + fib[i-2] return fib[n] function fib(n, solutions) if n in solutions return solutions[n] if n == 0 return 0 if n == 1 return 1 current = fib[n-1] + fib[n-2] solutions[n] = current return current This doesn’t seem useful…

RNA folding AUGCUAUAUAAACGCGAUACUAUACGCGAUAAUCGCGCGAGA ..........................................

RNA folding AUGCUAUAUAAACGCGAUACUAUACGCGAUAAUCGCGCGAGA ..........................................

RNA folding AUGCUAUAUAAACGCGAUACUAUACGCGAUAAUCGCGCGAGA ..........................................

RNA folding AUGCUAUAUAAACGCGAUACUAUACGCGAUAAUCGCGCGAGA ..........................................

RNA folding AUGCUAUAUAAACGCGAUACUAUACGCGAUAAUCGCGCGAGA .......................................... AUGCUAUAUAAACGCGAUACUAUACGCGAUAAUCGCGCGAGA ..........................................

RNA folding AUGCUAUAUAAACGCGAUACUAUACGCGAUAAUCGCGCGAGA .......................................... AUGCUAUAUAAACGCGAUACUAUACGCGAUAAUCGCGCGAGA ..........................................

RNA folding AUGCUAUAUAAACGCGAUACUAUACGCGAUAAUCGCGCGAGA .......................................... AUGCUAUAUAAACGCGAUACUAUACGCGAUAAUCGCGCGAGA ..........................................

RNA folding AUGCUAUAUAAACGCGAUACUAUACGCGAUAAUCGCGCGAGA .......................................... AUGCUAUAUAAACGCGAUACUAUACGCGAUAAUCGCGCGAGA .......................................... We’ve already solved this problem!

RNA folding function max_folds(seq) if length(seq) <= 1 return 0 current_max = 0 for i in 1..length(seq) for j in i..length(seq) if complement(seq[i], seq[j]) left = seq[1:i-1] middle = seq[i:j] right = seq[j+1:length(seq)] num_folds = 1 + max_folds(left + right) + max_folds(middle) if num_folds > current_max current_max = num_folds return current_max

RNA folding function max_folds(seq, solutions) if seq in solutions return solutions[seq] if length(seq) <= 1 return 0 current_max = 0 for i in 1..length(seq) for j in i..length(seq) if complement(seq[i], seq[j]) left = seq[1:i-1] middle = seq[i:j] right = seq[j+1:length(seq)] num_folds = 1 + max_folds(left + right, memo) + max_folds(middle, memo) if num_folds > current_max current_max = num_folds solutions[seq] = current_max return current_max

RNA folding AUGCUAUAUAAACGCGAUACUAUACGCGAUAAUCGCGCGAGA .......................................... With memoization: ~36 seconds 323,143 recursive calls (partial solutions stored) Without memoization: Before it had finished running, I wrote the memoization code…and then got tired of waiting for it to finish on the whole sequence 912,843 recursive calls on 16 (out of the 42) bases