Algorithm Design and Analysis Week 2 Data Structure TextBooks • Main Textbook • S.Sridhar, 2015 Design and Analysis of Algorithm. Oxford University. India. ISBN: 978-0-19-809369-5 LEARNING OUTCOMES • LO1 : Explain the fundamental concept of the analysis algorithm OUTLINE 1. Data Structures and Algorithms 2. Linked List 3. Stack 4. Queue 5. Priority Queue 6. Trees 7. Heaps 8. Graphs DATA STRUCTURES AND ALGORITHMS Data Structures and Algorithms • Algorithms process input data to generate meaningful output. • Data structure is a field of computer science that deals with the storage and retrieval of data. • Data is stored in the computer memory as a sequence of bits, as a computer can understand only binary digits. • Most programming languages support primitive data types • ADT is an abstract concept created by humans to simplify the calculation of a process through abstraction. • ADT is not directly recognized by the computer's processor, but a high level programming language can be used to implement ADT Data Structures and Algorithms • Differences between ADT and data structure Abstract Data Type Algorithmic notion and generalization of user-defined data types Not supported directly by programming languages Data Structure Programming notation May be supported directly by programming languages, e.g., arrays LINKED LIST Linked List • A list is an ADT that can be used to store a group of elements, which are of the same data type, which are of the same data type. • A linked list is a sequence of zero or more elements called nodes, each containing two kinds of information: some data and one or more links called pointers to other nodes of the linked list. (A special pointer called “null” is used to indicate the absence of a node’s successor.) Linked List (a) Node (b) Singly Linked List (c) Circular Singly Linked List (d) Doubly Linked List (e) Circular Doubly Linked List STACK Stack • ADT Stack is describing a stack of data. • Rule of stack is LIFO (Last In First Out) • Implementations of stack can use array or linked-list. • A stack in computer programming has three operations: o PUSH X (adding the data X into a stack) o POP (take the top element of a stack) o EMTPY (emptying the stack) Stack QUEUE Queue • Queue is an ADT that describes the data queue. • Rule of queue is FIFO (Last In First Out) • Implementations of queue can use array or linked-list • A queue in computer programming has three operations: o PUSH X (adding the data X into a queue) o POP (take the top element of a queue) o EMTPY (emptying the queue) Queue Circular Queue Tail Head ln l 1 l2 l3 PRIORITY QUEUE Priority Queue • Priority Queue is the further development of the concept of Queue. In priority queue each component consists of (key, value) • Priority Queue Example applications - Queue prospective passenger aircraft standby - Stock market Operation • InsertItem(k,v) Insert an element into a priority queue where k is the key and v is the value. • RemoveMin() As POP operation is queue, getting "up front" element in the queue (with regard to priority). • GetMinKey() Getting the smallest priority. • GetMinValue() Getting the smallest value. • Size() Getting the size of the queue. • IsEmpty() Check whether the queue contains something or not. Implementation • Unsorted List - InsertItem O(1) - RemoveKey(), GetMinKey() O(n) • Sorted List - InsertItem O(n) - RemoveKey(), GetMinKey() O(1) Sorting in Priority Queue • Selection Sort - Is the variation of PQ with an unsorted list - Adding a new element takes O (n) - Removing an element takes 1 +2 +3 +...+ n means O (n2) • Insertion Sort - Is the variation of PQ with a sorted list - Adding a new element takes 1 +2 +3 +...+ n means O (n2) - Removing an element takes O (n) TREE Tree • A tree is a widely-used data structure that emulates a hierarchical tree structure with a set of linked nodes. • There are a number of nodes connected on a hierarchical arrangement of the parent (parents) and child (children). • A child node must have one parent node. • A parent node can have multiple other nodes underneath it (child nodes). Tree Binary Tree • A binary tree is a tree data structure in which each node has at most two child nodes, usually distinguished as "left" and "right". • The maximum number of nodes in level k = 2k-1 • The maximum number of nodes in a Binary Tree depth k = 2k-1 Binary Tree Binary Tree • Binary tree can be implemented in array or linked-list. • Operation in binary tree : 1. Inserting data 2. Searching data 3. Deleting data 4. Sorting data Tree Traversal • Tree-traversal is the process of visiting (examining and/or updating) each node in a tree data structure, exactly once, in a systematic way. • Such traversals are classified by the order in which the nodes are visited. • Pre-order Traversal : (parent–left–right) • Level-order Traversal : (parent–left–right) (each level) • In-order Traversal : (left–parent–right) • Post-order Traversal : (left–right–parent) Pre – Order Traversal • Pre-order Traversal : (parent–left–right) Level – Order Traversal • Level-order Traversal : (parent–left–right) (each level) In – Order Traversal • In-order Traversal : (left–parent–right) Post – Order Traversal • Post-order Traversal : (left–right–parent) Tree Traversal Euler Tour Traversal + 1 16 11 10 x x 12 2 9 15 4 3 • • • • Pre-order : F, B, A, D, C, E, G, I, H In-order : A, B, C, D, E, F, G, H, I Post-order : A, C, E, D, B, H, I, G, F Level-order : F, B, G, A, D, I, C, E, H 13 14 – 2 3 5 8 7 6 a 1 b Implementation of Tree Depth First Search • Steps: 1. 2. 3. 4. 5. Set status to 1 (white color) to all nodes Enter root node to a stack, set status to 2 (orange color) Take top of the stack, set status to 3 (green color) Enter left child and right child to the stack, set status to 2 (orange color) Repeat step 3 until the stack is empty or searched data is found • Steps in the more "technical" term: 1. 2. 3. 4. PUSH root node POP node X dari stack PUSH all children of node X to the stack Repeat step 2 DFS: Example A B C E D F G DFS: Example A B C E D F G A DFS: Example A B E A C D F G Stack DFS: Example A B E B C D F G E Stack A DFS: Example A B E E C D F G Stack A B DFS: Example A B C E D C D F G E Stack A B DFS: Example A B E D C D F G E Stack A B C DFS: Example A B E A C D F G E Stack B C D DFS: Example A B E A C D F G Stack B C D E DFS: Example A B E A F C D F G G Stack B C D E DFS: Example A B E A C D F G G Stack B C D E F DFS: Example A B C E D F A G Stack B C D E F G Implementation of Tree Breadth First Search • Steps: 1. 2. 3. 4. 5. Set status to 1 (white color) to all nodes Enter root node to a queue, set status to 2 (orange color) Take the front of queue (bottom position), set status to 3 (green color) Enter left child and right child to the queue, set status to 2 Repeat step 3 until queue is empty or searched data is found. • Steps in the more "technical" term: 1. 2. 3. 4. PUSH root node a queue POP node X from queue PUSH all children of node X to the queue Repeat step 2. BFS: Example A B C E D F G BFS: Example A B C E D F G A BFS: Example A Queue B C E D F G A BFS: Example A Queue B E E C D F G B A BFS: Example A Queue B E E C D F G A B BFS: Example A Queue B E D C C D F G E A B BFS: Example A Queue B E A D C D F G C B E BFS: Example A Queue B G E F D C C D F G A B E BFS: Example A Queue B E G F C D F G D A B E C BFS: Example A Queue B E A G C D F G F B E C D BFS: Example A Queue B E A C D F G G B E C D F BFS: Example A Queue B E A C D F G B E C D F G HEAP Heap • • • • Heap is a complete Binary Tree Consists of internal nodes and external nodes Each internal node stores a value The values at each node is eligible to Minimum Heap or Maximum Heap Maximum/ Minimum Heap • Maximum Heap • If root is greater than left sub tree and right subtree • Minimum Heap • If root is less than left sub tree and right subtree • Further discussion of using the Minimum Heap Example of Minimum Heap Example of Maximum Heap Heap: Operation • InsertNode(k) • Insert a new node into a heap. • RemoveNode • Removing the root node then heapify the heap. • Empty Heap: Insert Node Operation Step 1 Step 2 Step 3 Heap: Delete Node Operation Step 1 Step 2 Step 3 Step 4 Heap Sort • Heap Sort is a sorting technique using the properties of heap. • Steps: • • • • All values are entered into the heap Heap then emptied by removing node one by one Note each removed node As a result, the numbers are sorted • Suppose we have a number of 21, 45, 15, 30 and 19 to sort. Heap Sort Implementation Step 1 Step 2 Step 3 Step 4 Complexity of Heap Sort • Complexity of Heap Sort algorithm consists: • Complexity in building heap • Complexity in removing all nodes in heap • Complexity of Heap Sort : f (n) (n 2 log n) GRAPH Graph • Graph is an ADT which consists of a set of nodes (points) and edges (lines). • An edge connects 1 or 2 nodes. • Edge, which only connect a node is called a loop edge. • Graph Type: o Directed Graph • Graph is trending. • Often also referred to by the term digraph. o Undirected Graph • Graph is not trending. Directed and Undirected Graph Directed Graph Undirected Graph Graph: Degree of Node • In degree x is the number of edges coming into node x. • Out degree x is the number of edges out of node x. • A node that all its outward edge is called source. • A node that all its leading edge into is called sink. • In Undirected Graph there is only 1 degree nodes, which is a lot of side (edge) which cross on that node. Representation Graph • Graph can be represented in: o Illustrations o Linked-list o Adjacency matrix (and Cost Matrix) • Adjacency matrix: the proximity between two nodes 1 if node i adjacent to node j A[i, j ] 0 if node i isnot adjacent to node j • Adjacency matrix is made by calculating the adjacency list Graph: Example Adjacency List A[1,2]=1 A[1,3]=0 A[1,4]=1 A[1,5]=0 A[1,6]=0 A[2,3]=0 A[2,4]=0 A[2,5]=0 A[2,6]=1 A[3,4]=0 A[3,5]=1 A[3,6]=1 A[4,5]=0 A[4,6]=0 A[5,6]=1 Graph: Example Adjacency Matrix 0 1 0 1 0 0 1 0 0 0 0 1 0 0 0 0 1 1 1 0 0 0 0 0 0 0 1 0 0 1 0 1 1 0 1 0 Graph: Example Adjacency Matrix 0 7 0 4 0 0 7 0 0 0 0 9 0 0 0 0 3 8 0 0 0 0 0 0 0 0 0 0 6 5 0 0 8 0 0 0 Graph: Cost Matrix • If the adjacency matrix shown in proximity, then the Cost Matrix seen in the distance. • If there is no edge connecting o Adjacency matrix is 0 o Cost Matrix valued ∞ Graph: Example Cost Matrix ∞7∞∞∞∞ 7∞∞∞∞∞ ∞∞∞∞∞8 4∞∞∞∞∞ ∞∞3∞6∞ ∞98∞5∞ References • Sridhar, S. (2015). Design and Analysis of Algorithms. Oxford University Press. Chapter 5. • V, R. (2020). Design and Analysis of Algorithms: The Learners Approach. • Data structures: Introduction to graphs [Video], www.youtube.com/watch?v=gXgEDyodOJU • Data structures: Properties of Graphs [Video], www.youtube.com/watch?v=AfYqN3fGapc • Data Structures: Stacks and Queues, www.youtube.com/watch?v=wjI1WNcIntg • Data Structures: Trees [Video], www.youtube.com/watch?v=oSWTXtMglKE • Priority Queue Introduction [Video], www.youtube.com/watch?v=wptevk0bshY