CompSci 100 12.1 Problem: finding subsets l See CodeBloat APT, requires finding sums of all subsets  Given {72, 33, 41, 57, 25} what is sum closest (not.

Slides:



Advertisements
Similar presentations
Algorithms (and Datastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck.
Advertisements

CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
CSE 373 Graphs 1: Concepts, Depth/Breadth-First Search
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
CompSci 100e Program Design and Analysis II April 19, 2011 Prof. Rodger CompSci 100e, Spring
Graph Searching CSE 373 Data Structures Lecture 20.
Graphs Chapter Chapter Contents Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Tyler Robison Summer
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Dan Grossman Spring 2010.
1 Spanning Trees Lecture 20 CS2110 – Spring
Graph & BFS.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Graphs. Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
CPS Graphs, the Internet, and Everything
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
CS 146: Data Structures and Algorithms July 21 Class Meeting
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Compsci 100, Fall ’.1 Views of programming l Writing code from the method/function view is pretty similar across languages  Organizing methods.
MA/CSSE 473 Day 12 Insertion Sort quick review DFS, BFS Topological Sort.
COSC 2007 Data Structures II Chapter 14 Graphs III.
Representing and Using Graphs
Prims’ spanning tree algorithm Given: connected graph (V, E) (sets of vertices and edges) V1= {an arbitrary node of V}; E1= {}; //inv: (V1, E1) is a tree,
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Software Design 8.1 A Rose by any other name…C or Java? l Why do we use Java in our courses (royal we?)  Object oriented  Large collection of libraries.
Compsci 100, Spring Graphs, the Internet, and Everything
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.
Compsci 100, Spring Graphs, the Internet, and Everything
CompSci 100e 12.1 Is there a Science of Networks? l What kinds of networks are there? l From Bacon numbers to random graphs to Internet  From FOAF to.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
CompSci 100e 11.1 Sorting: From Theory to Practice l Why do we study sorting?  Because we have to  Because sorting is beautiful  Example of algorithm.
CS 61B Data Structures and Programming Methodology Aug 5, 2008 David Sun.
1 Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: b feasible b locally optimal.
Where’s the title? You gotta search for it!. PotW Solution String s = new Scanner(System.in).next(); int[] prev = new int[s.length() * 2 + 2]; Arrays.fill(prev,
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Graphs Upon completion you will be able to:
CompSci Graphs, the Internet, and Everything
7. Graph Traversal Describe and compare depth-first and breadth-first graph searching, and look at the creation of spanning trees Contest Algorithms: 7.
Compsci 101, Fall LWoC l Review Recommender, dictionaries, files  How to create recommendations in order? food.txt  Toward a Duke eatery-recommender.
CPS Graphs: Structures and Algorithms l How do packets of bits/information get routed on the internet  Message divided into packets on client.
CompSci 100e 10.1 Is there a Science of Networks? l What kinds of networks are there? l From Bacon numbers to random graphs to Internet  From FOAF to.
Graphs and Paths : Chapter 15 Saurav Karmakar
CompSci 100e 12.1 Sorting: In 2 Slides l Why do people study sorting?  Because we have to  Because sorting is beautiful  Example of algorithm analysis.
Chapter 05 Introduction to Graph And Search Algorithms.
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
CSE373: Data Structures & Algorithms Lecture 14: Topological Sort / Graph Traversals Dan Grossman Fall 2013.
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.
Bottom up meets Top down
Graphs, the Internet, and Everything
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Graphs Representation, BFS, DFS
Chapter 11 Graphs.
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
From bit to byte to char to int to long
Presentation transcript:

CompSci Problem: finding subsets l See CodeBloat APT, requires finding sums of all subsets  Given {72, 33, 41, 57, 25} what is sum closest (not over) 100?  How do we do this in general? l Consider three solutions (see also SubsetSums.java)  Recursively generate all sums: similar to backtracking Current value part of sum or not, two recursive calls  Use technique like sieve to form all sums Why is this so fast?  Alternative solution for all sums: use bit patterns to represent subsets What do 10110, 10001, 00111, 00000, and represent? How do we generate sums from these representations?

CompSci From subsets to graphs with bits l We’ll consider SequenceSync APT  What is a “vertex” in the graph? Where are arcs?  For state-0, we have {1,5,4,2} for transitions l We’ll consider a graph in which vertices are sets of states  Start with every possible state in our initial vertex

CompSci Word ladder

CompSci Vocabulary l Graphs are collections of vertices and edges (vertex also called node)  Edge connects two vertices Direction can be important, directed edge, directed graph Edge may have associated weight/cost l A vertex sequence v 0, v 1, …, v n-1 is a path where v k and v k+1 are connected by an edge.  If some vertex is repeated, the path is a cycle  A graph is connected if there is a path between any pair of vertices NYC Phil Boston Wash DC LGALAX ORD DCA $186 $412 $1701 $441

CompSci Graph questions/algorithms l What vertices are reachable from a given vertex?  Two standard traversals: depth-first, breadth-first  Find connected components, groups of connected vertices l Shortest path between any two vertices (weighted graphs?)  Breadth first search is storage expensive  Dijkstra’s algorithm is efficient, uses a priority queue too! l Longest path in a graph  No known efficient algorithm l Visit all vertices without repeating? Visit all edges?  With minimal cost? Hard!

CompSci Vocabulary/Traversals l Connected?  Connected components? Weakly connected (directionless)  Degree: # edges incident a vertex indegree (enter), outdegree (exit) l Starting at 7 where can we get?  Depth-first search, envision each vertex as a room, with doors leading out Go into a room, mark the room, choose an unused door, exit –Don’t go into a room you’ve already been in (see mark) Backtrack if all doors used (to room with unused door)  Rooms are stacked up, backtracking is really recursion  One alternative uses a queue: breadth-first search

CompSci Breadth first search l In an unweighted graph this finds the shortest path between a start vertex and every vertex  Visit every node one away from start  Visit every node two away from start This is every node one away from a node one away  Visit every node three away from start, … l Put vertex on queue to start (initially just one)  Repeat: take vertex off queue, put all adjacent vertices on  Don’t put a vertex on that’s already been visited (why?)  When are 1-away vertices enqueued? 2-away? 3-away?  How many vertices on queue?

CompSci General graph traversal COLLECTION_OF_VERTICES fringe; fringe = INITIAL_COLLECTION; while (!fringe.isEmpty()) { Vertex v = fringe.removeItem(QUEUE_FN); if (! MARKED(v)) { MARK(v); VISIT(v); for each edge (v,w) { if (NEEDS_PROCESSING(w)) Add w to fringe according to QUEUE_FN; }

CompSci Breadth-first search l Visit each vertex reachable from some source in breadth-first order l Like level-order traversal Queue fringe; fringe = {v}; while (!fringe.isEmpty()) { Vertex v = fringe.dequeue(); if (! getMark(v)) { setMark(v); VISIT(v); for each edge (v,w) { if (MARKED(w)) fringe.enqueue(w); } l How do we change to make depth-first search?  How does the order visited change?

CompSci How do we search in SequenceSync? l Given a vertex (collection of states) how do we determine what vertex it’s connected to?  Consider each transition from each state in our vertex (remember this is a set of states)  This yields a new set of states/vertex 1-away from vertex l What does the code look like for bfs? When do we stop? while (q.size() != 0){ TreeSet current = q.remove(); for(int k=0; k < 4; k++){ TreeSet next = new TreeSet (); for(int val : current){ next.add(matrix[val][k]); } q.add(next); // if not already seen }

CompSci Problems with approach? l Creating sets and looking them up in map takes time  This solution times out, how to improve it?  l Don’t represent set of states explicitly, use sequence of bits  Similar to CodeBloat, advantages? Disadvantages?  How do we determine when we’re done?  How to store distances (how is array like a map?) l Rewrite solution to be efficient using int for set  Initial set is all ones, how to make this?

CompSci Greedy Algorithms l A greedy algorithm makes a locally optimal decision that leads to a globally optimal solution  Huffman: choose two nodes with minimal weight, combine Leads to optimal coding, optimal Huffman tree  Making change with American coins: choose largest coin possible as many times as possible Change for $0.63, change for $0.32 What if we’re out of nickels, change for $0.32? l Greedy doesn’t always work, but it does sometimes l Weighted shortest path algorithm is Dijkstra’s algorithm, greedy and uses priority queue

CompSci Dijkstra’s Shortest Path Algorithm l Similar to breadth first search, but uses a priority queue instead of a queue. Code below is for breadth first search q.dequeue(vertex w) foreach (vertex v adjacent to w) if (distance[v] == INT_MAX) // not visited { distance[v] = distance[w] + 1; q.enqueue(v); } l Dijkstra: Find minimal unvisited node, recalculate costs through node q.deletemin(vertex w) foreach (vertex v adjacent to w) if (distance[w] + weight(w,v) < distance[v]) { distance[v] = distance[w] + weight(w,v); q.insert(vertex(v, distance[v])); }

CompSci Shortest paths, more details l Single-source shortest path  Start at some vertex S  Find shortest path to every reachable vertex from S l A set of vertices is processed  Initially just S is processed  Each pass processes a vertex After each pass, shortest path from S to any vertex using just vertices from processed set (except for last vertex) is always known l Next processed vertex is closest to S still needing processing SAB CE process B SAB CE E 7 7 S A B C process C E 7 7 S A B C

CompSci A Rose by any other name…C or Java? l Why do we use Java in our courses (royal we?)  Object oriented  Large collection of libraries  Safe for advanced programming and beginners  Harder to shoot ourselves in the foot l Why don't we use C++ (or C)?  Standard libraries weak or non-existant (comparatively)  Easy to make mistakes when beginning  No GUIs, complicated compilation model

CompSci Why do we learn other languages? l Perl, Python, PHP, mySQL, C, C++, Java, Scheme, ML, …  Can we do something different in one language? Depends on what different means. In theory: no; in practice: yes  What languages do you know? All of them.  In what languages are you fluent? None of them l In later courses why do we use C or C++?  Closer to the machine, we want to understand the machine at many levels, from the abstract to the ridiculous Or at all levels of hardware and software  Some problems are better suited to one language What about writing an operating system? Linux?

CompSci C++ on two slides l Classes are similar to Java, compilation model is different  Classes have public and private sections/areas  Typically declaration in.h file and implementation in.cpp Separate interface from actual implementation Good in theory, hard to get right in practice  One.cpp file compiles to one.o file To create an executable, we link.o files with libraries Hopefully someone else takes care of the details l We #include rather than import, this is a preprocessing step  Literally sucks in an entire header file, can take a while for standard libraries like iostream, string, etc.  No abbreviation similar to java.util.*;

CompSci C++ on a second slide l We don't have to call new to create objects, they can be created "on the stack"  Using new creates memory "on the heap"  In C++ we need to do our own garbage collection, or avoid and run out of memory (is this an issue?) l Vectors are similar to ArrayLists, pointers are similar to arrays  Unfortunately, C/C++ equate array with memory allocation  To access via a pointer, we don't use. we use -> l Streams are used for IO, iterators are used to access begin/end of collection  ifstream, cout correspond to Readers and System.out

CompSci How do we read a file in C++ and Java? Scanner s = new Scanner(new File(“data.txt”)); TreeSet set = new TreeSet (); while (s.hasNext()){ String str = s.next(); set.add(str); } myWordsAsList = new ArrayList (set); string word; set unique; ifstream input(“data.txt”); while (input >> word){ unique.insert(word); } myWordsAsVector = vector (unique.begin(), unique.end()); l What are similarities? Differences?

CompSci How do we read a file in C? FILE * file = fopen("/u/ola/data/poe.txt","r"); char buf[1024]; char ** words = (char **) malloc(5000*sizeof(char **)); int count = 0; int k; while (fscanf(file,"%s",buf) != EOF){ int found = 0; // look for word just read for(k=0; k < count; k++){ if (strcmp(buf,words[k]) == 0){ found = 1; break; } if (!found){ // not found, add to list words[count] = (char *) malloc(strlen(buf)+1); strcpy(words[count],buf); count++; } l What if more than 5000 words? What if string length > 1024? What if?  What is complexity of this code?