Min Heap Update E.g. remove smallest item 1. Pop off top (smallest) 3

Slides:



Advertisements
Similar presentations
Heuristic Search techniques
Advertisements

CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
CSE 380 – Computer Game Programming Pathfinding AI
CS 221 Guest lecture: Cuckoo Hashing Shannon Larson March 11, 2011.
Heapsort. 2 Why study Heapsort? It is a well-known, traditional sorting algorithm you will be expected to know Heapsort is always O(n log n) Quicksort.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Chocolate Bar! luqili. Milestone 3 Speed 11% of final mark 7%: path quality and speed –Some cleverness required for full marks –Implement some A* techniques.
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
Recursion, Complexity, and Sorting By Andrew Zeng.
Heapsort CSC Why study Heapsort? It is a well-known, traditional sorting algorithm you will be expected to know Heapsort is always O(n log n)
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
A few m3 tips. Speed Tuning 1.Algorithm 2.Data structures 3.Low level code string streetName1, streetName2; if (streetName1 != streetName2) {... int streetId1,
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 12/6/2006 Lecture 24 – Profilers.
Administration CI meetings resume next week, as usual Some TAs in labs, plus Dr. Betz –Go to your lab as usual –If your TA is not there, ask for help from.
1 Finding Shortest Paths with A* Search A* search operates similarly to Dijkstra’s algorithm, but extends the cost function to include an estimated distance.
Intro To Algorithms Searching and Sorting. Searching A common task for a computer is to find a block of data A common task for a computer is to find a.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
Administration: Upcoming Due Dates Milestone 1: due Monday, Feb. 1 at 5 pm Code Review on milestone 1: –Coming soon; due Monday, Feb. 8 WD1: graphics proposal.
A few m3 tips. Street Segment Length / Travel Time Need to compute in double precision –Otherwise too much round off See piazza.
July 10, 2016ISA's, Compilers, and Assembly1 CS232 roadmap In the first 3 quarters of the class, we have covered 1.Understanding the relationship between.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Breadth-First Search Graph Algorithm Type #3.
“Generic Programming” ECE 297
Measuring Where CPU Time Goes
Search: Advanced Topics Computer Science cpsc322, Lecture 9
Binary Search Trees One of the tree applications in Chapter 10 is binary search trees. In Chapter 10, binary search trees are used to implement bags.
May 17th – Comparison Sorts
Breadth-First Search: Complexity
Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008
Programming Abstractions
Efficient implementations of Alignment-based algorithms
Heuristic Search Introduction to Artificial Intelligence
Lesson Objectives Aims Understand the following “standard algorithms”:
CPU Efficiency Issues.
Cse 373 May 15th – Iterators.
Profiling for Performance in C++
Quick Start Guide for Visual Studio 2010
Search: Advanced Topics Computer Science cpsc322, Lecture 9
More Graph Algorithms.
Discussion section #2 HW1 questions?
2008/12/03: Lecture 20 CMSC 104, Section 0101 John Y. Park
Data Structures and Algorithms
Dr. David Matuszek Heapsort Dr. David Matuszek
i206: Lecture 14: Heaps, Graphs intro.
Binary Search Trees One of the tree applications in Chapter 10 is binary search trees. In Chapter 10, binary search trees are used to implement bags.
Graphs.
Using a Debugger 1-Jan-19.
Debugging Taken from notes by Dr. Neil Moore
Heapsort.
Simple Sorting Methods: Bubble, Selection, Insertion, Shell
Sorting "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
BugHint: A Visual Debugger Based on Graph Mining
Debugging Taken from notes by Dr. Neil Moore
Multithreading Why & How.
Not guaranteed to find best answer, but run in a reasonable time
Efficiently Estimating Travel Time
BFS: Min. Path Issues What happened? Node 1 was re-expanded
Engineering Analysis: Detailed Design Phase
slides created by Ethan Apter
CSE 373 Data Structures and Algorithms
Heapsort.
CSE 1020:Software Development
B-Trees.
Heapsort.
Heaps & Multi-way Search Trees
CO 303 Algorithm Analysis and Design
Lecture 27: More Graph Algorithms
Presentation transcript:

Min Heap Update E.g. remove smallest item 1. Pop off top (smallest) 3 20 36 40 35 37 60 50 2. Move bottom (large) item to top

Min Heap Update E.g. remove smallest item 3. Compare to children; swap if necessary 50 20 36 40 35 37 60

Min Heap Update E.g. remove smallest item O(log N) levels 20 4. Repeat for each level 50 36 40 35 37 60 O(log N) levels  O(log N) work

BFS and Dijkstra Demo

Better than Dijkstra’s Algorithm?

Dijkstra: Not So Smart! Why are we examining nodes in the wrong direction? Wasted CPU time!

Ideas? Only look at nodes that take us closer to the destination? distToDest(node) < distToDest (predecessor) Too severe – can result in no path, or not shortest path

A* Algorithms Enhancements to Dijkstra’s algorithm Use domain-specific heuristics E.g. we are working in a map Can estimate which graph nodes are closer vs. further from dest Incorporate into the wavefront sorting/ordering Look at the most promising partial solutions first “Best-first search”

Can go around blockages (e.g. 401) A* Search Mostly searches in the right direction, but will explore some in other directions Can go around blockages (e.g. 401)

A* Heuristics Sort wavefront by? Ideas? n2 bestTime = 45 s source dest Estimate (heuristic)

A* Implementation We are changing the wavefront sorting Not the bestTime found to a node from the source That must be accurate (not a guess) Or we wouldn’t know when we’ve found a better path to a node struct WaveElemAstar { Node *node; int edgeID; // ID of edge used to reach this node double travelTime; // Total travel time to reach node double myGoodIdeaOfValueToSortOn; }

A* Demo

Milestone 3 Performance Tuning Tips

Speed Tuning Algorithm Data structures Low level code

Debugging Algorithms Start with a very simple test case Adjacent intersections! Step / breakpoint code in debugger Make sure it does what you expect!

Debugging Algorithms Try a slightly harder case Two intersections apart Step / breakpoint code in debugger again! Make sure code behaving exactly as you expect

Graphics to Visualize Algorithm

Bigger Cases: Graphics Helps Hard to see large amounts of data in debugger Use graphics to animate / visualize #define VISUALIZE // Comment out to turn off while (wavefront.size() != 0) { // Get id of node to evaluate … #ifdef VISUALIZE record_node_visited (node_id); #endif ... application.run ( … // Can visualize nodes you explored with ezgl Don’t leave visualization on in your final submission!

Measuring Where CPU Time Goes Profiling Code Measuring Where CPU Time Goes

My Code Is Too Slow – Why? Look at code  what O() is it? Loading: O(N) unavoidable and OK O(N2)  not good if N can get big Look-ups If you’ll do something N times, try to keep it O(1) or O(log N)

My Code is Complex! Can’t figure out O() Or O() looks OK, but still not fast enough Profile! Measure where the time goes

Simple Profiling: Manual Random Sampling Run the debugger Stop it with Debug  Pause Look at the subroutine and line where you paused Examine the call stack to see how you got there Continue execution with Debug  Continue More time in a routine  higher probability of stopping there Usually stop in same routine  found the problem

Detailed Profiling: gprof Tool Randomly samples the function your program is in ~ every 1 ms Also records how it got there (call stack / call graph) Then summarizes the output for you How is this random sampling done? Program asks to be interrupted ~1000x / second by operating system Each interrupt  record function you are in

gprof Tool: How to Use Compile your program with the right options Select profile configuration or make CONF=profile Adds –pg option to compiler  instruments exe Turns off function inlining  all function calls exist  easier to interpret

gprof Tool: How to Use Run program normally ./mapper Collects statistics, stores in big file (gmon.out) Program runs only a little slower (~30%) Run gprof to summarize / interpret output gprof mapper > outfile.txt Reads gmon.out, generates readable outfile.txt Even better: can visualize (graphics) gprof mapper gmon.out | gprof2dot . py -s | xdot - ECE 297 Profiling Quick Start Guide

Example: Extract Negatives #include <vector> using namespace std; vector<int> extract_negatives (vector<int>& numbers) { vector<int> negatives; int i = 0; while(i < numbers.size()) { if(numbers[i] < 0) { negatives.push_back(numbers[i]); numbers.erase(numbers.begin() + i); } else { i++; //Next element } return negatives; Takes 30 s when given an 800,000 element vector. Too slow  why?

Visualized Call Graph Extract negatives called once by main Takes 71% of time Type equation here. push_back() Estimated 37% of time This is an over-estimate: sample-based profiling isn’t perfect Erase called over 640,000 times Takes 53% of time  biggest problem