CS1114 Assignment 3. 1 Previously, on Assignment 2. 2 Linked lists

Similar documents
CS 206 Introduction to Computer Science II

CS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.

Linked lists. Prof. Noah Snavely CS1114

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

Some major graph problems

Graphs & Digraphs Tuesday, November 06, 2007

CSE 332: Data Abstractions Lecture 15: Topological Sort / Graph Traversals. Ruth Anderson Winter 2013

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

CS 310 Advanced Data Structures and Algorithms

Announcements Problem Set 4 is out!

CSE 373: Data Structures and Algorithms

CS24 Week 8 Lecture 1

Multidimensional Arrays & Graphs. CMSC 420: Lecture 3

Lecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this

CS61BL. Lecture 5: Graphs Sorting

Fundamental Algorithms

CSE 100: GRAPH ALGORITHMS

Algorithm Design and Analysis

csci 210: Data Structures Graph Traversals

LECTURE 17 GRAPH TRAVERSALS

CSC263 Week 8. Larry Zhang.

Data Structures Question Bank Multiple Choice

Algorithms and Data Structures, Academic Year 2010/2011

Lesson 1 Introduction to Path Planning Graph Searches: BFS and DFS

Lecture 3: Stacks & Queues

Data Structure. IBPS SO (IT- Officer) Exam 2017

CSE 100: GRAPH SEARCH

UNIT 6A Organizing Data: Lists. Last Two Weeks

INSTITUTE OF AERONAUTICAL ENGINEERING

(Refer Slide Time: 02.06)

22.1 Representations of graphs

csci 210: Data Structures Graph Traversals

CS 206 Introduction to Computer Science II

Trees. Arash Rafiey. 20 October, 2015

Algorithm Design and Analysis

Lecture 9 Graph Traversal

Module 11: Additional Topics Graph Theory and Applications

CS171 Final Practice Exam

Parallel Pointers: Graphs

Introduction p. 1 Pseudocode p. 2 Algorithm Header p. 2 Purpose, Conditions, and Return p. 3 Statement Numbers p. 4 Variables p. 4 Algorithm Analysis

GRAPHS Lecture 17 CS2110 Spring 2014

Graph Algorithms. Definition

CS24 Week 4 Lecture 2

Graphs. The ultimate data structure. graphs 1

Procedural Programming & Fundamentals of Programming

We don t have much time, so we don t teach them [students]; we acquaint them with things that they can learn. Charles E. Leiserson

Hi everyone. I hope everyone had a good Fourth of July. Today we're going to be covering graph search. Now, whenever we bring up graph algorithms, we

Direct Addressing Hash table: Collision resolution how handle collisions Hash Functions:

QUIZ QuickSort (Ch.7)

Separate Compilation and Namespaces Week Fall. Computer Programming for Engineers

CS103L PA4. March 25, 2018

Trees Algorhyme by Radia Perlman

CS171 Final Practice Exam

MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)

Outlines: Graphs Part-2

UNIT 6A Organizing Data: Lists Principles of Computing, Carnegie Mellon University

CS 216 Exam 1 Fall SOLUTION

Introduction to Graphs. CS2110, Spring 2011 Cornell University

In either case, remember to delete each array that you allocate.

Theory of Computing. Lecture 4/5 MAS 714 Hartmut Klauck

CS Data Structures and Algorithm Analysis

CS2 Algorithms and Data Structures Note 9

Computer Science Foundation Exam

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

CS 112 Final May 8, 2008 (Lightly edited for 2012 Practice) Name: BU ID: Instructions

Final Examination CSE 100 UCSD (Practice)

Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

Binary Trees Due Sunday March 16, 2014

(notes modified from Noah Snavely, Spring 2009) Memory allocation

Chapter 9 STACK, QUEUE

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Data Structure Advanced

Cpt S 122 Data Structures. Data Structures


Breadth First Search. cse2011 section 13.3 of textbook

I want an Oompa-Loompa! screamed Veruca. Roald Dahl, Charlie and the Chocolate Factory

Link to starter code:

Algorithms: Lecture 10. Chalmers University of Technology

Outline. Graphs. Divide and Conquer.

(Re)Introduction to Graphs and Some Algorithms

12 Abstract Data Types

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

TIE Graph algorithms

Graph Search Methods. Graph Search Methods

CS8391-DATA STRUCTURES QUESTION BANK UNIT I

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Graph Search Methods. Graph Search Methods

Lecture 26: Graphs: Traversal (Part 1)

Graphs. Data Structures 1 Graphs

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

EECE.2160: ECE Application Programming

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

Lecture 25 Spanning Trees

Breadth First search and Depth First search Finding connected components

Do you remember what is the most powerful tool for managing complexity?

Information Science 2

Objective Questions for Online Practical Exams under CBCS Scheme Subject: Data Structure-I (CS-113)

W4231: Analysis of Algorithms

Transcription:

CS1114 Assignment 3 out: February 25, 2013 due: March 8, 2013 by 5pm 1 Previously, on Assignment 2 In the last assignment we implemented several robust ways of finding the lightstick center. In this assignment, we will do even better, by using graph traversal to find all connected components of red pixels, then identifying the largest one and finding its centroid. 2 Linked lists In order to find connected components, we need to implement a graph traversal algorithm. But to implement graph traversal, we need a way of maintaining a todo list that keeps track of unvisited vertices. Two options for implementing the todo list are a queue and a stack: in this part of the assignment, we will implement queues and stacks in Matlab using doubly-linked lists; this is your first task. In class, we talked about how to implement a linked list that stores single integers; however, for images, it is easier to identify vertices with two numbers (the row and column). Thus, our linked lists will be a little different: they will store two integers in each cell. Thus, each cell will have four elements (and will therefore take up four adjacent memory elements): 1. A pointer to the previous cell in the list. 2. The row of the pixel. 3. The column of the pixel. 4. A pointer to the next cell in the list. This time, the header will have four elements: 1. A pointer to the first cell in the list. 2. A pointer to the last cell in the list. 3. The size of the list. 4. A pointer to the first free cell. 1

(You won t have to worry about updating the fourth element the free cell in this assignment.) Thus a list with the elements (17,42), (10,20), (18,36) conceptually looks like: This list could be represented in memory as: where green entries are backwards pointers, red entries are forwards pointers, blue entries are values, and grey entries represent header information (X s are free cells). Remember that a 0 in a pointer slot is a null pointer signifying the beginning or end of the list. We provide you with a function to create and list and allocate a new element: newlinkedlist(): This function returns a new array to use as the memory for storing a linked list. The header will be properly initialized to [ 0, 0, 0, first free cell ] two null pointers and a size of 0, all signifying an empty list (and a pointer to the first free memory cell). To use this function, you can enter, for instance: >> list = newlinkedlist(); getnewlistcell(list): This function takes a list, allocates space for a new cell (of size 4), and returns the index of the new cell. For instance, we can enter: >> [newindex, list] = getnewlistcell(list); if we need to create a new cell. Note that it is important to include list in the output variables, or else the consistency of the list will not be maintained! Your first task is to implement a function for printing out the contents of a linked list. The output of this function should look like this (given as input the list above): >> printlinkedlist(list); [17, 42] -> [10, 20] -> [18, 36] 2

= Implement the printlinkedlist function in the file printlinkedlist.m. You may find the fprintf function helpful. To create a linked list for testing, you can use the newlinkedlist and listinsertatstart functions. For instance: >> list = newlinkedlist(); >> list = listinsertatstart(list, 18, 36); >> list = listinsertatstart(list, 10, 20); >> list = listinsertatstart(list, 17, 42); will create the linked list pictured above. Next, you ll need to implement a couple of functions for inserting and deleting elements. To get you started, we provide you with functions for inserting and deleting from the start of a list: listinsertatstart(list, row, col): This function takes a list, a row, and a column, and inserts a cell containing (row,col) at the beginning of the list. The function returns the new list. For instance, to insert (17,42) at the start of a list called list, we could do: >> list = listinsertatstart(list, row, col) Again, it is important that we assign the result of the function back to the variable list. The listinsertatstart function demonstrates how different cases are handled when inserting an element into a list you can refer to it when implementing the listinsertatend function below. listdeleteatstart(list): This function takes a list and deletes the first element in the list. The function returns the new list. For instance: >> list = listdeleteatstart(list) The listdeleteatstart function demonstrates how different cases are handled when deleting an element from a list you can refer to it when implementing the listdeleteatend function below. You will need to implement functions for inserting and deleting from the end of the list. You will also need to implement the listisempty function, which returns 1 if the list is empty, and 0 otherwise. = Implement two functions, listinsertatend and listdeleteatend, for insertion and deletion from the end of a list. listinsertatend takes three arguments (a list, a row, and a column) and returns the new list, and listdeleteatend takes a list and return the new list. Be careful to handle all the different cases properly, and remember 3

that each cell has both a forwards and a backwards pointer. Headers are provided for these functions in listinsertatend.m and listdeleteatend.m. = Implement listisempty, in listisempty.m. To test your functions, you can create a list as described above. We will also provide you with additional functions for use in testing. 3 Stacks and queues We can now implement stacks and queues using linked lists. For stacks, we need two functions: stackpush and stackpop (as well as listisempty, implemented above). For queues, we also need two functions: enqueue and dequeue. The stackpush and enqueue functions take a list and a row and column of a pixel, and return the updated stack or queue. The stackpop and dequeue functions take a list, and return the appropriate row and column, as well as the new list. Here are a few examples demostrating their expected functionality: >> myqueue = newlinkedlist(); >> myqueue = enqueue(myqueue, 1, 2); >> myqueue = enqueue(myqueue, 10, 20); >> [row, col, myqueue] = dequeue(myqueue); % row, col should be 1, 2 >> [row, col, myqueue] = dequeue(myqueue); % row, col should be 10, 20 >> mystack = newlinkedlist(); >> mystack = stackpush(mystack, 1, 2); >> mystack = stackpush(mystack, 10, 20); >> [row, col, mystack] = stackpop(mystack); % row, col should be 10, 20 >> [row, col, mystack] = stackpop(mystack); % row, col should be 1, 2 = Implement the five functions stackpush, stackpop, enqueue, and dequeue, in terms of the insertion and deletion functions in Section 2 (these five functions will be fairly short). Headers for these functions are provided for you in listisempty.m, stackpush.m, stackpop.m, enqueue.m, and dequeue.m. 3 Connected components A connected component of an undirected graph is a maximal set of vertices which has the property that you can go from any vertex to every other by traveling through the edges in the graph. A connected component in the graph can be found using depth-first search (DFS) or breadth-first search (BFS), as described in lecture. To do graph traversal, we need a todo list to keep track of unvisited vertices. DFS is implemented using a stack as the todo list, and BFS is implemented using queues. 4

1 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0 1 1 0 1 0 0 0 0 1 Figure 1: Use this example to test your implementation of connected-component-finding using BFS and DFS. We encourage you to use the builtin Matlab connected components function bwlabel to debug your code. It takes two parameters, as follows: L = bwlabel(image, 4) The number 4 indicates the neighborhood system (left, down, right, up). The size of the output matrix L is same as the input image, but each connected component is marked with a unique number, starting from 1 and going until the number of connected components in the graph is reached. In other words, all points in blob 1 will have a value of 1, all points in blob 2 will have a value of 2, and so on and so forth. You can use another Matlab function called label2rgb to visualize different connected components: >> L = bwlabel(image, 4); >> RGB = label2rgb(l, jet, blue ); You can use Matlab s help command to see more information about these functions. 4 What To Do You can find templates for all of the functions you need to implement in the /courses/cs1114/student files/a3 directory. Start by copying all of the files in this folder to your myfiles directory. 1. Implement the linked list functions described in Section 2: printlinkedlist, listinsertatend, listdeleteatend, and listisempty. It might be helpful to look at the implementation of listinsertatstart. 2. Implement the queue and stack functions described in Section 3: stackpush, stackpop, enqueue, and dequeue. 5

3. Implement and test connected components on images, using the 4-connected neighborhood system. You must use both DFS and BFS. In your DFS implementation, use the code for stacks given above, and in your BFS implementation use the code for queues. The input to these functions will be a binary image, a root pixel location, a label number, and a image labeled with the previously detected connected components, and the output will be a labeled image in which every pixel in the same component as the root pixel has the value of label. Note that you don t have to do this for each connected component in the image we take care of this for you with functions called CC DFS and CC BFS. You can test your code by calling CC DFS or CC BFS, which take as input a binary image, and return a labeled image where each component has a different label (as well as the number of components). We provide you with a function ccexample that returns the example binary image above, which can be used for testing, e.g.: >> [labeled, num_components] = CC_DFS(ccExample()); >> % CC_DFS calls your CC_DFS_student function There are also several binary images in the student files/a3 directory for use in testing. (a) Your DFS implementation should have the following prototype: function [ labeled ] = CC_DFS_student(bimage, root_row, root_col, label, labeled) Place your code in CC DFS student.m. (b) Your BFS implementation should have the following prototype: function [ labeled ] = CC_BFS_student(bimage, root_row, root_col, label, labeled) Place your code in CC BFS student.m. 4. Using your DFS implementation, compute the connected components in an input image and return only the largest one. Your implementation should have the following prototype: function labeled = big_red_student(bimage) Place your code in big red student.m. Note that it might be helpful to know that given a matrix A, the Matlab command A == k returns a binary image where all elements of A with value k are 1, and the rest of the elements are 0. The find function might also be helpful here. 6

5. Using your big red region code and your median vector student.m code from Assignment 2, find the median center of the biggest red region. Your implementation should have the following prototype: function [r,c] = big_red_median_vector_student(img) Place your code in big red median vector student.m. 6. Write a function which drives a robot based on the position of the largest connected component and its area. The goal will be to move the robot to keep the lightstick at a fixed position and size: if the lightstick center is to the left of the center of the image, the robot should turn left, and if the lightstick center is to the right of the center of image, the robot should turn right. If the area of the lightstick is larger than some constant size (say 30,000 pixels), the robot should move backwards; if it is smaller than this size, the robot should move forwards. You will need to use the following robot functions robotinit, robotdrivestraight, and robotturn. Place your code in CC robots student.m. 7