COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf ·...
Transcript of COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf ·...
![Page 1: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/1.jpg)
![Page 2: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/2.jpg)
Data structures
Data structures are a way of organizing collections of data in the computer's memory
![Page 3: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/3.jpg)
Data structures
Data structures are a way of organizing collections of data in the computer's memory
Examples of data structures we have seen so far
Arrays
![Page 4: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/4.jpg)
Data structures
Data structures are a way of organizing collections of data in the computer's memory
Examples of data structures we have seen so far
Objects
![Page 5: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/5.jpg)
Data structures
Data structures are a way of organizing collections of data in the computer's memory
Examples of data structures we have seen so far
Networks of cities (Assignment 4), a social network (last class)
![Page 6: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/6.jpg)
Data structures
Data structures are a way of organizing collections of data in the computer's memory
Examples of data structures we have seen so far
ArraysObjectsNetworks of cities (Assignment 4), a social network (last class)Linked Lists (today's class)Hashtables (today's class)
![Page 7: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/7.jpg)
Linked Lists
![Page 8: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/8.jpg)
Linked Lists
This is a visual representation of a linked list using the Person objects from the social network
![Page 9: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/9.jpg)
Linked Lists
This is a visual representation of a linked list using the Person objects from the social network
![Page 10: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/10.jpg)
Linked Lists
This is a visual representation of a linked list using the Person objects from the social network
![Page 11: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/11.jpg)
Linked Lists
This is a visual representation of a linked list using the Person objects from the social network
![Page 12: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/12.jpg)
Linked Lists
This is a visual representation of a linked list using the Person objects from the social network
![Page 13: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/13.jpg)
Linked Lists
This is a visual representation of a linked list using the Person objects from the social network
![Page 14: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/14.jpg)
Linked Lists
This is a visual representation of a linked list using the Person objects from the social network
![Page 15: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/15.jpg)
Linked Lists
This is a visual representation of a linked list using int values
![Page 16: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/16.jpg)
Going through the components of a LinkedList
We need to define a class for each element in the list: the Node class
The Node class should have:
A pointer to some data. We will call it its value. For simplicity, we will hold int numbers in our LinkedListA pointer to the next element in the list. Unsurprisingly, we will call it next
public class Node{ private int value; private Node next;
// add a constructor here
// add getter and setter methods here }
123456789
![Page 17: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/17.jpg)
Going through the components of a LinkedList
We need to define a class for doing operations on the list: the LinkedList class
The LinkedLsit class should have:
A pointer to the first element in the list. We will call it start.All the methods with the operations we want to make on lists
public class LinkedList{ private Node start;
// add a constructor here
// add Insertion, search and deletion methods here }
12345678
![Page 18: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/18.jpg)
Inserting elements in a linked list
Inserting an element at the beginning
![Page 19: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/19.jpg)
Inserting elements in a linked list
Inserting an element at the end
![Page 20: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/20.jpg)
Inserting elements in a linked list
Inserting an element somewhere in the middle
![Page 21: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/21.jpg)
Inserting elements in a linked list
Removing an element from the list
![Page 22: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/22.jpg)
Inserting elements in a linked list
Removing an element from the list
![Page 23: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/23.jpg)
Some remarks about LinkedList
A LinkedList allows us to add or remove elements without having to copy the whole data structureIn a LinkedList with n elements, all operations ( search, insertion, deletion) take at most n comparisons
In big O notation
Operations in a LinkedList have a running time of ==>
![Page 24: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/24.jpg)
To try for yourself
Based on the LinkedList and Node classes, implement a DoublyLinkedListIn a doubly linked list, all the nodes have a reference to its predecessor : the previous element in the list.
Implement all the insert, search and delete operations
![Page 25: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/25.jpg)
Hash tables
![Page 26: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/26.jpg)
Hash tables
Hash tables can be viewed as dictionaries
![Page 27: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/27.jpg)
We use a hashing function to determine where an element should go
![Page 28: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/28.jpg)
Implementing a Hashtable
We use an array to store the entries in our dictionary
We need to implement the hashing function to determine where an element should go
public class Hashtable{ /* * the entries could be of any type * here, we use the String entries, for example. */ private String[] entries;
// add a constructor here
// add Insertion, search and deletion methods here
/* * The hashing function an index in the entries * array for the given element */ public int hashFunction( String name ){ // calculate an int ( the hash values) // for the String name } }
123456789101112131415161718192021
![Page 29: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/29.jpg)
Hash tables
The hash function maps data to hash values (positions in the array)
![Page 30: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/30.jpg)
In this case, the hashing function maps the first letter of a name to a position in the array
![Page 31: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/31.jpg)
When two or more elements have the same hash value, a collision occurs.
![Page 32: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/32.jpg)
Hash tables with multiple elements per entry
A first step to deal with collisions, is to make each entry point to another data structure; e.g. aLinkedList
![Page 33: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/33.jpg)
Implementing a Hashtable with LinkedLists for each position in thearray
We use an array to store the entries in our dictionary
public class HashtableL{ /* * the entries could be of any type. * here, we use a LinkedList to keep * multiple entries in each poistion * of the array. */ private LinkedList[] entries;
// add a constructor here
// add Insertion, search and deletion methods here
/* * The hashing function an index in the entries * array for the given element */ public int hashFunction( String name ){ // calculate an int ( the hash values) // for the String name } }
1234567891011121314151617181920212223
![Page 34: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/34.jpg)
Hash tables with multiple elements per entry
A first step to deal with collisions, is to make each entry point to another data structure; e.g. aLinkedList
![Page 35: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/35.jpg)
Some remarks about Hash tables
A Hashtable allows us to add or remove elements quickly by making use of a hash functionTo deal with multiple elements in a single location in the dictionary, we combine the hashing function with a LinkedList
In big O notation
Operations in a Hash Table have a running time of ==> , where k is the
number of elements in each position of the entries array.
Operations in a Hash Table have a running time of ==>
![Page 36: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/36.jpg)
Resources
Classes and Objects:http://docs.oracle.com/javase/tutorial/java/javaOO/The Shoelace Algorithm:http://en.wikipedia.org/wiki/Shoelace_formulaSuggested reading:How to think like a Computer Scientist, Chapter 11
![Page 37: COMP 202- Week 10 Class 2 - McGill CIMcim.mcgill.ca/~gamboa/cs202/Material/class19/week10-2.pdf · Networks of cities (Assignment 4), a social network (last class) Linked Lists (today's](https://reader034.fdocuments.us/reader034/viewer/2022042404/5f18593847a7ab036b42e2f6/html5/thumbnails/37.jpg)
← →
1 / 41
Go to slide: Drawing Tools