1.03k likes | 1.05k Views
Learn about FIFO queues in data structures, Queue<E> interface methods, and Stack<E> operations. Solve swinging monkeys and chemistry problems with Java implementations.
E N D
Take-Home Lab #04 CS1020 – Data Structures And Algorithms 1 AY2015-16 Semester 2
Queues are “one-way”. Only insert from the back, can only take the head. (FIFO) FIFO = First-In First-Out Big “Queue of Stacks” 1 7 9 1 3 1 3 1 5 3 100 4 Dequeue Enqueue 8 17 5 22 5 4 Head Tail Queue<E> = new LinkedList<E>(); http://docs.oracle.com/javase/8/docs/api/java/util/Queue.html Stack and Queue
Interface Queue<E> <<interface>> Queue<E> • //see Java API + add(E) : boolean + peek() : E + poll() : E … Stack and Queue
Interface Queue<E> <<interface>> Queue<E> • //see Java API add(E) + add(E) : boolean + peek() : E + poll() : E … Queue Stack and Queue
Interface Queue<E> <<interface>> Queue<E> • //see Java API poll() + add(E) : boolean + peek() : E + poll() : E … Queue Stack and Queue
Class Stack<E> Stack<E> • elements: LinkedList<E> + push(E) : void + pop() : E + peek() : E + getSize() : int + isEmpty() : boolean Stack and Queue
Class Stack<E> Stack<E> • elements: LinkedList<E> + push(E) : void + pop() : E + peek() : E + getSize() : int + isEmpty() : boolean 1 3 5 4 1 Stack and Queue
push(10) • Class Stack<E> Stack<E> • elements: LinkedList<E> 10 + push(E) : void + pop() : E + peek() : E + getSize() : int + isEmpty() : boolean 1 3 5 4 1 Stack and Queue
peek() Returns 10 • Class Stack<E> Stack<E> • elements: LinkedList<E> 10 + push(E) : void + pop() : E + peek() : E + getSize() : int + isEmpty() : boolean 1 3 5 4 1 Stack and Queue
pop() Returns 10 • Class Stack<E> Stack<E> • elements: LinkedList<E> 10 + push(E) : void + pop() : E + peek() : E + getSize() : int + isEmpty() : boolean 1 3 5 4 1 Stack and Queue
Class Stack<E> Stack<E> • elements: LinkedList<E> publicintgetSize() { returnelements.size(); } publicbooleanisEmpty() { returnthis.getSize() == 0; } + push(E) : void + pop() : E + peek() : E + getSize() : int + getIndex() : int + isEmpty() : boolean Stack and Queue
Problem 1 Swinging Monkeys
Count the number of possible swings: • Monkeys can swing from one tree to another directly as long as there is no tree in between that is taller than or have the same height as either one of the two trees. • Given the sequence of tree heights, determine the number of pair of trees that the monkeys can swing from and to. Swinging Monkeys
5 Trees: 19m – 17m – 20m – 20m – 20m Output: 5 Swinging Monkeys
Naïve solution: • For all trees, count how many trees that the monkey can swing to from that tree. • Maintain two for loops, one for the source and one for the destination: for (inti = 0; i < n; i++) { for(intj = i + 1; j < n; j++) { if(canSwing(i,j)) { count++; } } } Swinging Monkeys
privatebooleancanSwing(intfrom, intto) { for(inti = from + 1; i < to; i++) { if(trees[i] >= Math.min(from, to)) { returnfalse; } } returntrue; } Swinging Monkeys
5 Trees: 19m – 17m – 20m – 20m – 17m Observe: from the tree before the Blue tree, the Monkey cannot jump to the tree after the Blue tree Swinging Monkeys
After we process tree i, we can forget all the trees before ithat are shorter than i. • What will be the property of the sequence of trees that are not forgotten? • Decreasing order from the tree with smallest index. • Why? • Suppose it is not decreasing in sequence: • After we process Blue, we can forget Yellow. • Yellow would have been removed from the sequence. Swinging Monkeys
Swinging Monkeys • Property of the Sequence: • Decreasing Order of Height • At the start of the processing of the next tree,the Monkey can jump from all tree in the sequence to the next tree • If the next tree is higher than some elements,these elements in the sequence cannot jump beyond the next tree • Discussions: • What Data Structure do we need to implement? • Stack: why? • Because we need to remember the previous elements in order
Problem 2 Chemistry
Given a set of mappings and chemical formula: • Calculate the molecule mass of the formula C 12 H 1 N 14 O 16 (NH4)2CO3 Problem Description Chemistry
Let’s work on a few examples based on the given mapping (NH4)2CO3 C 12 H 1 N 14 O 16 CH4OH ((CH3)CH4C3H8)4 Problem Description Chemistry
Let’s work on a few examples based on the given mapping (NH4)2CO3 14 + 4 x 1 C 12 H 1 N 14 O 16 CH4OH ((CH3)CH4C3H8)4 Problem Description Chemistry
Let’s work on a few examples based on the given mapping (NH4)2CO3 18 * 2 C 12 H 1 N 14 O 16 CH4OH ((CH3)CH4C3H8)4 Problem Description Chemistry
Let’s work on a few examples based on the given mapping 36+ CO3 12 + 3 x 16 C 12 H 1 N 14 O 16 CH4OH ((CH3)CH4C3H8)4 Problem Description Chemistry
Let’s work on a few examples based on the given mapping 36+ CO3 60 C 12 H 1 N 14 O 16 CH4OH ((CH3)CH4C3H8)4 Problem Description Chemistry
Let’s work on a few examples based on the given mapping 36+ 60 C 12 H 1 N 14 O 16 CH4OH ((CH3)CH4C3H8)4 Problem Description Chemistry
Let’s work on a few examples based on the given mapping (NH4)2CO3 Mr = 96 C 12 H 1 N 14 O 16 CH4OH ((CH3)CH4C3H8)4 Problem Description Chemistry
Let’s work on a few examples based on the given mapping (NH4)2CO3 Mr = 96 C 12 H 1 N 14 O 16 CH4OH 12 + 4 x 1 + 16 + 1 ((CH3)CH4C3H8)4 Problem Description Chemistry
Let’s work on a few examples based on the given mapping (NH4)2CO3 Mr = 96 C 12 H 1 N 14 O 16 CH4OH 33 ((CH3)CH4C3H8)4 Problem Description Chemistry
Let’s work on a few examples based on the given mapping (NH4)2CO3 Mr = 96 C 12 H 1 N 14 O 16 Mr = 33 CH4OH ((CH3)CH4C3H8)4 Problem Description Chemistry
Let’s work on a few examples based on the given mapping (NH4)2CO3 Mr = 96 C 12 H 1 N 14 O 16 Mr = 33 CH4OH ((CH3)CH4C3H8)4 12 + 3 x 1 Chemistry
Let’s work on a few examples based on the given mapping (NH4)2CO3 Mr = 96 C 12 H 1 N 14 O 16 Mr = 33 CH4OH ((CH3)CH4C3H8)4 15 Chemistry
Let’s work on a few examples based on the given mapping (NH4)2CO3 Mr = 96 C 12 H 1 N 14 O 16 Mr = 33 CH4OH (15 + CH4C3H8)4 12 + 4 x 1 + 3 x 12 + 8 x 1 Chemistry
Let’s work on a few examples based on the given mapping (NH4)2CO3 Mr = 96 C 12 H 1 N 14 O 16 Mr = 33 CH4OH (15 + CH4C3H8)4 60 Chemistry
Let’s work on a few examples based on the given mapping (NH4)2CO3 Mr = 96 C 12 H 1 N 14 O 16 Mr = 33 CH4OH (15 + 60)4 Chemistry
Let’s work on a few examples based on the given mapping (NH4)2CO3 Mr = 96 C 12 H 1 N 14 O 16 Mr = 33 CH4OH Mr = 300 ((CH3)CH4C3H8)4 Problem Description Chemistry
? What data structure should I use for: Mass Mapping Formula Processing Stack HashMap Chemistry
HashMap<K, V> hashMapName= newHashMap<K, V>(); HashMap HashMap<Character, Integer> massMapping= newHashMap<Character, Integer>(); Setting Keys and Values Retrieving Values Returns 12 massMapping.put(‘C’, 12); massMapping.get(‘C’); Returns 1 massMapping.put(‘H’, 1); massMapping.get(‘H’); Returns 14 massMapping.put(‘N’, 14); massMapping.get(‘N’); https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html
What should my stack contain? Mass of each Atom The Elements OR Stack https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html
What should my stack contain? Mass of each Atom The Elements Stack We will use a stack of integers CH4 Stack https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html
What should my stack contain? Mass of each Atom The Elements Stack We will use a stack of integers CH4 12 Stack https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html
What should my stack contain? Mass of each Atom The Elements Stack We will use a stack of integers 1 CH4 12 Stack https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html
What should my stack contain? Mass of each Atom The Elements Stack We will use a stack of integers 4 1 CH4 12 Stack https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html
What should my stack contain? Mass of each Atom The Elements Stack We will use a stack of integers Total = 12 + 4 = 16 4 1 CH4 12 Stack https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html
How do I store the formula? As a character array String nextLine = sc.nextLine(); char[] formula = nextLine.toCharArray(); ? How do I process the formula? Loop through all characters processInput(formula); Chemistry
publicvoid run() { Scanner sc = new Scanner(System.in); intn = sc.nextInt(); sc.nextLine(); initializeMassMapping(sc, n); //implement it String nextLine = sc.nextLine(); char[] formula = nextLine.toCharArray(); processInput(formula); inttotal = accumulate(); //will be defined later System.out.println(total); sc.close(); } Chemistry
privatevoidprocessInput(char[] formula) { for(charc : formula) { //what should I do??? } } Next character can be one of these: What you should do if you encounter it: ( Open Bracket Start New “Session” ) Close Bracket Acummulate Current “Session” X Atom Name Push Its Mass n Multiply Top of The Stack by n Number of atoms (or molecules) Chemistry
privatevoidprocessInput(char[] formula) { for(charc : formula) { if (c == '(') { //open bracket results.push(-1); //start a new “session” } elseif (c == ')') { //close bracket intsum = accumulate(); //acummulate is a “helper” //then push the sum into the stack } elseif (Character.isDigit(c)) {//it’s a number //multiply the top of the stack. How? } else { //then c must be a ??? //push the corresponding atom’s mass } } } Chemistry
privateint accumulate() { intresult = 0; inttop = results.pop(); while(/* top is not -1 and stack is not empty */){ //add current top to result //update top to be next element in stack } returnresult; } Chemistry