Discussion section #2 HW1 questions?

Slides:



Advertisements
Similar presentations
Bellman-Ford algorithm
Advertisements

* 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 / 05 / 2008 Instructor: Michael Eckmann.
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.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
1 Graphs Algorithms Sections 9.1, 9.2, and Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A graph G = (V, E) –V: set of vertices (nodes) –E: set.
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.
Pointers OVERVIEW.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
– Graphs 1 Graph Categories Strong Components Example of Digraph
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.
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
Shortest Paths.
Graphs Chapter 15 introduces graphs which are probably the most general and commonly-used data structure. This lecture introduces heaps, which are used.
Programming with ANSI C ++
Graphs Representation, BFS, DFS
Sorted Linked List Same objective as a linked list, but it should be sorted Sorting can be custom according to the type of nodes Offers speedups over non-sorted.
Lecture 13 Shortest Path.
Chapter 7: Greedy Algorithms
CSE 2331/5331 Topic 9: Basic Graph Alg.
Csc 2720 Instructor: Zhuojun Duan
Cse 373 May 8th – Dijkstras.
Minimum Spanning Trees and Shortest Paths
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Introduction to Linked Lists
CS330 Discussion 6.
Short paths and spanning trees
More Graph Algorithms.
Discussion section #2 HW1 questions?
Shortest Paths with Dynamic Programming
Graphs Representation, BFS, DFS
Graph Algorithm.
Linked List Intro CSCE 121 J. Michael Moore.
Graphs Chapter 13.
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Shortest Paths.
Finding a Eulerian Cycle in a Directed Graph
CSCE 411 Design and Analysis of Algorithms
Graphs.
Outline This topic covers Prim’s algorithm:
Chapter 11 Graphs.
Graphs Part 2 Adjacency Matrix
Shortest Path Algorithms
CSCE 411 Design and Analysis of Algorithms
ITEC 2620M Introduction to Data Structures
Fundamental Data Structures and Algorithms
Algorithms: Design and Analysis
CSE 373: Data Structures and Algorithms
Shortest Path Algorithm for Weighted Non-negative Undirected Graphs
CSE332: Data Abstractions Lecture 17: Shortest Paths
Shortest Paths.
CSE 417: Algorithms and Computational Complexity
Dijkstra's Shortest Path Algorithm
CSE 373: Data Structures and Algorithms
Important Problem Types and Fundamental Data Structures
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Applications of BFS CSE 2011 Winter /17/2019 7:03 AM.
Lecture 12 Shortest Path.
Linked List Intro CSCE 121.
CSE 373: Data Structures and Algorithms
Lecture 14 Minimum Spanning Tree (cont’d)
Chapter 24: Single-Source Shortest-Path
Graph Algorithm.
Presentation transcript:

Discussion section #2 HW1 questions? HW2: find highest-weight path on a DAG Shortest (minimum-weight) path algorithms C++ basics

HW2: highest-weight path on a DAG

HW2: highest-weight path on a DAG Here’s the algorithm in words:

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 With and without fixed start/endpoints Output: Path score Beginning and end vertex labels The labels of all the edges on the path, in order

HW2: highest-weight path on a DAG Parts 1 & 2: finding highest weight paths for a provided graph Convert visual graph to text file format (one line for each vertex and each edge) Read in file Find & report HWP

HW2: highest-weight path on a DAG Parts 3: finding highest weight path for a genome sequence Convert sequence to a WDAG in text file format (one line for each vertex and each edge) Read in file Find & report HWP

Minimum-weight path on a DAG Similar to the homework Looking for the minimum instead of the maximum If there are no negative weights, then the shortest path is technically weight 0 (single node) Otherwise, update vertex weights in depth order as normal

What if graph has cycles? More difficult (can’t order nodes by depth) Bellman-Ford algorithm (for given start vertex) 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) Tutorial and dynamic programming implementation here.

What if graph has no negative edges? Less difficult (we can visit some nodes fewer times) Dijkstra’s algorithm (for given start vertex) 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)

C++: heap vs. stack memory Objects created inside a function stored here by default Automatically deleted once the function runs to completion Fixed size – stack overflow if you run out of space Much faster to allocate Local data, parameter passing, etc. Heap Must use the “new” operator when creating objects to store them here Must be manually deleted More memory can be added to the heap as needed Use heap memory for large objects, or if you don’t know how much space you need Slower to allocate Global/shared data, large objects For more info, see Stack Overflow thread here.

C++: arrays & vectors Arrays Built-in language construct for a contiguous, indexable sequence of elements Fixed size, determined upon declaration More efficient For arrays of objects, the object class must have a default constructor Vectors Template class implemented as a dynamic array Grows and shrinks dynamically, and memory is managed automatically Less efficient Objects do not require a default constructor For those uncomfortable with pointers, vectors offer a level of abstraction; they can be passed to other functions/referred to by value, so you don’t strictly need to use pointers to interact with them, unlike arrays. However, do note that passing by value means you are copying the contents of the vector to pass them on to the function, so be careful where you use this. For more info, see Stack Overflow threads here and here, as well as this tutorial.

C++: sorting & comparators std::sort() – standard C++ sorting function (docs) Usage: std::sort(first, last) [default sorting] std::sort(first, last, comp) [custom sorting] `first` and `last` are random-access iterators indicating the range of elements to be sorted, [first, last) `comp` is a comparator object that describes how items should be compared for sorting E.g. a comparator for two Suffix objects might look like struct compSuffix { bool operator() (const Suffix& a, const Suffix& b) { return <true when a comes before b> } } For more info on custom comparators, see the Stack Overflow thread here.

C++: iterators Iterator: something that allows you to Iterate over a sequence Represent a sequence or subsequence Access elements of a sequence Introductory tutorial here, and C++ documentation on random-access iterators here Iterator functions include `begin()` (returns beginning position of container) and `end()` (returns end position of container) Pointers are a type of iterator For more info on pointers, see tutorial here, GameDev post here, and Stack Overflow thread here.