Graphs, Continued ECE 297.

Slides:



Advertisements
Similar presentations
Container Classes A container class is a data type that is capable of holding a collection of items. In C++, container classes can be implemented as.
Advertisements

ECE 454 Computer Systems Programming Compiler and Optimization (I) Ding Yuan ECE Dept., University of Toronto
Elementary Data Structures: Part 2: Strings, 2D Arrays, Graphs
ITEC 320 Lecture 12 Higher level usage of pointers.
Zhang Hongyi CSCI2100B Data Structures Tutorial 2
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
1 Classes with Pointer Data Members (II) Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230.
CS 1114: Data Structures – memory allocation Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
1  Caches load multiple bytes per block to take advantage of spatial locality  If cache block size = 2 n bytes, conceptually split memory into 2 n -byte.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Linked lists and memory allocation Prof. Noah Snavely CS1114
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
Administrative Stuff ECE 297. Administration Milestone 0: –Submit by Friday at 5 pm –Demo in lab this week –Write your name on the board when ready to.
Feature Engineering Week 3 Video 3. Feature Engineering.
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.
Databases with PHP A quick introduction. Y’all know SQL and Databases  You put data in  You get data out  You can do processing on it very easily 
11 Getting Started with C# Chapter Objectives You will be able to: 1. Say in general terms how C# differs from C. 2. Create, compile, and run a.
Milestone 3: Finding Routes ECE 297. Directions: How?
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Lecture 10 Inheritance “Absolute C++” Chapter 14.
Tutorial 2 Abstract Data Types 1. Admin Stuffs Some of you request to change tutorial class… – Transfer within my own classes (T3, T4, T5, T6) is fine.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
CIT 590 Intro to Programming First lecture on Java.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Dynamic Allocation Joe Meehean. Dynamic Allocation Memory for local objects is automatically created and reclaimed memory is created for it at beginning.
Documentation. Your documentation must fit the needs of your audience. It’s always better to say one thing that is useful, as opposed to many things that.
“The perfect project plan is possible if one first documents a list of all the unknowns.” Bill Langley.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Monday, Feb 3, 2003Kate Gregory with material from Deitel and Deitel Week 5 Lab 1 comments Hand in Lab 2 Questions from Last Week Classes continued Lab.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
1 CS161 Introduction to Computer Science Topic #9.
CS 11 C++ track: lecture 3 Today const and const -correctness operator overloading.
Debugging Computer Networks Sep. 26, 2007 Seunghwan Hong.
Templates “Generic Programming” ECE Templates A way to write code once that works for many different types of variables –float, int, char, string,
Lesson 2: Reading a program. Remember: from yesterday We learned about… Precise language is needed to program Actors and Classes Methods – step by step.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 10 – C: the heap and manual memory management.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
February 28, 2005 Introduction to Classes. Object Oriented Programming An object is a software bundle of related variables and methods. Software objects.
CSCI 156: Lab 11 Paging. Our Simple Architecture Logical memory space for a process consists of 16 pages of 4k bytes each. Your program thinks it has.
ECE297 TA GUIDE Project supervision. Agenda M0 feedback Project overview M1 overview Project supervision.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Announcements Partial Credit Due Date for Assignment 2 now due on Sat, Feb 27 I always seem to be behind and get tons of daily. If you me and.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
REST API Design. Application API API = Application Programming Interface APIs expose functionality of an application or service that exists independently.
Memory Management.
“Generic Programming” ECE 297
Depth First Seach: Output Fix
Introduction to Python
Dr. Bernard Chen Ph.D. University of Central Arkansas
Administrative Items ECE 297.
Functionality & Performance in Milestone 1
Tutorial 8 An optional eighth tutorial will be held the week of March 6. This tutorial will give you practice with and feedback on oral presentation and.
Explaining issues with DCremoval( )
Milestone 3: Finding Routes
ICOM 5016 – Introduction to Database Systems
Min Heap Update E.g. remove smallest item 1. Pop off top (smallest) 3
Functionality & Performance in Milestone 1
Presentation transcript:

Graphs, Continued ECE 297

Review: Map as a Graph Intersections  Nodes Street Segments  Edges unique id and unique name Street Segments  Edges unique id only ids for intersections & street segments overlap  remember which is which in your code Bloor & Yonge 1 215,000 street segments 24,500 streets 1 Bay & College 3 College & Spadina 4 2 3 4 DVP & unknown(20) 5 2 Bloor & Bay

Graph Type? Undirected or directed? One way streets  directed A two way street makes 2 edges

Might also have “in_edge” list if you want/need it Graph Representation? Adjacency List or Adjacency Matrix? 1 2 3 4 Adjacency Matrix Adjacency List Node 0: out_edge {3} Node 1: out_edge {0, 3, 2} Node 2: out_edge {3, 4} … To Node 0 1 2 3 4 0 0 0 0 1 0 1 1 0 1 1 0 2 0 0 0 1 1 3 0 1 0 0 0 4 0 0 1 0 0 Might also have “in_edge” list if you want/need it From Node

Memory Complexity N intersections (N ~ 166,000 for Toronto) Adjacency matrix: N rows x N columns Memory required: O(N2) 26 GB (for 1 byte / entry)! Adjacency list: N lists Average: 4 ints / list O(N) 2.6 MB! 1 2 3 4 Adjacency Matrix To Node 0 1 2 3 4 0 0 0 0 1 0 1 1 0 1 1 0 2 0 0 0 1 1 3 0 1 0 0 0 4 0 0 1 0 0 Adjacency List Node 0: out_edge {3} Node 1: out_edge {0, 3, 2} Node 2: out_edge {3, 4} … From Node

libstreetsdatabase: More Than Connectivity Intersections (Nodes) id name lattitude, longitude StreetSegments (Edges) Street id  What street are you part of? This has a name Speed limit One way (from node  to node) Curvepoints  shape of street segment

StreetSegment: Curve Points Not all StreetSegments (city blocks) are straight If not straight, have CurvePoints  list of (lat,lon) points followed between intersections

libstreetdatabase: Streets & POI list of StreetSegments unique id unique name (e.g. “Yonge”) Points of Interest id location non-unique name (e.g. “Tim Horton’s) Natural Features ponds, parks, etc. Not used in milestone1

Graph Reference Chapter 22 of Introduction to Algorithms by Cormen et al

Milestone 1 List of functions to implement in m1.h Comments say what each should do Good practice: documentation is in your code! Header file: comments say how to use Not how it is built Detailed unit tests in ece297exercise These give the detailed specification “Test-driven development” Most test functionality Some also test speed  too slow  fail One runs valgrind to check memory OK

Milestone 1 Unit Tests main.cpp (made by unit tests) // Load anything you want here load_map (“toronto.bin”); int id = find_intersection_id_from_name (“Yonge & Bloor”); if (id != 22333) { <your code failed the test, record it> } // lots more tests // clean up here; all memory should be de-allocated close_map ();

load_map What should I do in load_map ()? 3 basic approaches // Load anything you want here load_map (string map_name); What should I do in load_map ()? 3 basic approaches load_map does nothing Implement all your higher-level api calls by calling lower level api (libstreetdatabaseapi) directly Stateless Often easier to code & debug But may be slow for some calls load_map builds everything you could ever want Never have to call lower-level api again You have full control  make beautiful, fast data structures

load_map Blended approach load_map creates some new data structures // Load anything you want here load_map (string map_name); Blended approach load_map creates some new data structures Use them for api calls that need them for speed Call lower level api when it is fast enough All 3 approaches are reasonable Your choice which to use If you fail speed tests, change your approach load_map not automatically speed tested Spec: keep speed interactive Your TA will notice in the demo if it is very slow (> 1 s or so) Otherwise OK

Clean Up main.cpp (made by unit tests) // Load anything you want here load_map (“toronto.bin”); int id = find_intersection_id_from_name (“Yonge & Bloor”); if (id != 22333) { <your code failed the test, record it> } // lots more tests // clean up here; all memory should be de-allocated close_map (); ece297exercise runs valgrind on a load_map(), use, close_map() test Must free all memory or will lose marks

Automatic Memory Checking Memory checking: valgrind slowdown ~10x Testing: want fast & thorough Small test case: almost as thorough as big St. Helena

load_map  How Do I Get Its Data? main.cpp (made by unit tests) // Load anything you want here load_map (“toronto.bin”); int id = find_intersection_id_from_name (“Yonge & Bloor”); if (id != 22333) { <your code failed the test, record it> } // lots more tests // clean up here; all memory should be de-allocated close_map (); ? Easy way  global variable

But Aren’t Global Variables Bad? Yes, if you overuse them E.g. could pass argument but use global variable to avoid typing! But almost all programs have some Or something very similar Limit their number Group related data into classes (e.g. StreetGraph?)  organizes data and avoids lots of namespace pollution StreetGraph find_intersection_id_from_name (“B & Y“); load_map() close_map()

Global Variable Pitfalls StreetGraph *streetGraph; // Pointer to your data! int find_intersection_id_from_name (string intersection_name) { streetGraph->answerMe (intersection_name); // What if streetGraph isn’t loaded yet? // Crash! delete streetGraph; // Thought I should clean up, but my teammates will be // surprised when they try to use streetGraph next! } Anyone can use / call functions of / delete our global object Would be nice if we could check it was set up first

Object-Oriented Alternative Design pattern: singleton Have one loaded copy of a certain object Many parts of the program need it StreetGraph find_intersection_id_from_name (“B & Y“); load_map() close_map() When function asks for access Return a pointer to the singleton object But can check that it is loaded first

StreetGraph as a Singleton class StreetGraph { private: static StreetGraph *theObjectPtr; // One copy per class public: StreetGraph (string mapName) { if (theObjectPtr != NULL) { cerr << “StreetGraph already loaded!” << endl; . . . // take some action; else { ... // Load up stuff theObjectPtr = this; } static StreetGraph *getObjectPtr ( ) { if (theObjectPtr == NULL) { cerr << “StreetGraph not loaded yet!” << endl; . . . // could end the program, or could load stuff up return (theObjectPtr); Can call without an object Replaces our global variable: like a controlled access global variable

Calling without an object  use full name Singleton StreetGraph.h class StreetGraph { ... public: ~StreetGraph () { ... // Clean everything up theObjectPtr = NULL; // Says we’re not loaded } somefile.cpp #include “StreetGraph.h” void myFunc ( ) { StreetGraph *streetGraph = StreetGraph::getObjectPtr (); streetGraph->someQuery ( ... ); ... } Calling without an object  use full name

Further Reading Common problems, and how to map them to software designs E.g. singleton pattern Inspired by patterns in architecture

Team Formation Dr. Tallman

ECE297 Tutorials, Jan 21 & Jan 23 Your team will meet your Communication Instructor (CI) and schedule a weekly 30-minute meeting beginning the following week. Wed, Jan 21, 1-3pm, students go to GB404 Fri, Jan 23, 9-11am, students go to GB412 Fri, Jan 23, 3-5pm, students go to GB412 If you do not have a full team formed by these dates, come to your scheduled tutorial, and Dr. Tallman will assist you.