Regarding homework 9 Many low grades To recover half of the points lost on Map methods Download test program used (from home page) Fix any problem(s) Bring to lab on Friday may 5 Original code with attached grading sheet Hard copy of revised map.cpp with changed or added code highlighted Run your program with provided test case for one of the instructors Be ready to answer questions about the changes you made
Using a vector to store the vertices What is the efficiency of getvnum(vname) O(n) What is the efficiency of getvname(vnum) O(1)
How many edges does a graph with v vertices have?
Storing the edges A graph has between 0 and v2 edges Adjacency matrix based on storing information about all possible edges Adjacency list based on storing information about only the existing edges
Storing the edges of a graph
Vertex table 1 2 3 4 A B C D E 1 2 3 4 1 2 3 4 0 1 2 3 4 1 2 3 4 Adjacency list Adjacency matrix
Vertex table 1 2 3 4 A B C D E 1 2 3 4 1 2 3 4 0 1 2 3 4 1 2 4 2 1 2 3 4 1 1 3 2 Adjacency list Adjacency matrix
Storing the edges of a digraph
0 A 1 B 2 C 3 D 4 E
0 A 1 B 2 C 3 D 4 E How do we find out if v1 is adjacent to v2? How do we find out if v1 is adjacent from v2?
Comparing adjacency matrix and adjacency lists Given that a graph has v vertices and e edges How much memory space is needed? What is the efficiency of Add an edge from v1 to v2? Is there an edge from v1 to v2? How many vertices are adjacent to v1? How many vertices are adjacent from v1?
standard template library (STL) Most programs need to store a collection of like elements Most programming languages now have types (classes) that programmers can use Java: collection classes Python: list and map C++ added the standard template library in 1998 Stl was updated in 2011 and 2014
Stl components Containers iterators algorithms
containers Are templates for classes that hold a collection of like items a class to hold a specific type can be created from a template User of the class decides type of items to be stored Alternative to using typedef to define the type of items Ex: vector<t> is a template <T> is a type parameter that needs to replaced with a specific type vector<int> numbers; vector<string> words;
Any questions about homework 10?
Some Stl containers Sequence containers Vector List Forward_list - added in 2011 Container adapters Stack Queue PRIORITy_queue Associative containers Map Set Unordered associative containers Unordered_map - added in 2011 Unordered_set - added in 2011
Sequence container classes share many methods
Using stl container classes to implement a graph Vertex table Vector<string>; Adjacency matrix vector<vector<bool>> edges; Adjacency lists ???
Using stl container classes to implement a graph Vertex table Vector<string>; Adjacency matrix vector<vector<bool>> edges; Adjacency lists Vector<vector<int>> edges; Vector<list<int>> edges; Vector<forward_list<int> edges; Which is best? and why?
iterators Stl container classes have iterator classes associated with them Ex: <vector> defines a type: vector<T>::iterator <list> defines a type: list<t>::iterator An iterator provides access to an element in a container Provide same functionality as vector indices and linked list node pointers Iterator objects associated with all container classes have 3 methods Iter++ // iter now gives access to the “next” element *iter // the value of the element accessed by iter == and !== // do two iterators access the same element? Iterator objects associated with some container classes have additional methods Iterators associated with which sequence container class can be decremented?
How are iterators used? Moving through a container one element at a time all container classes (except adapters like stack and queue) have two methods Begin() // returns an iterator that accesses the container’s first element End() // returns an iterator that is positioned after the container’s last element Dereferencing it is an error Vector<string> words; // add strings to words vector<string>::iterator iter = words.begin(); // or auto iter = words.begin(); for (iter; iter != words.end(); iter++) { cout << *iter << endl; } Suppose words was defined as: forward_list<string> Iterators are the connection between containers and algorithms
<algorithm> Contains functions that operate on a range of elements in a container The range is defined by iterators Ex: words.Begin() and words.end() specifies all elements in words Some functions defined in <algorithm> Find Find_if min_element For_each Sort
Using sort Can only be used with containers that have random access iterators Two parameter version Iterators specifying range of elements to sort Uses < operator to compare elements Three parameter version Third parameter is a function that compares 2 elements