DATA STRUCTURES & ALGORITHMS Presentation Team 4 Back Next Table of contents I III Introduction(Hung) II Stack and Queue(Hung & Tung) Brief Explanation of data structure and algorithms Explain and examples about ADT and FIFO Sorting algorithms (Minh) Two network shortest path algorithms (Tien) Explain and examples about sorting algorithms IV Explain and examples about network path algorithms I. Introduction Back Next What is data structures ? ● A data structure is not only used for organizing the data. It is also used for processing, retrieving, and storing data. There are different basic and advanced types of data structures that are used in almost every program or software system that has been developed. So we must have good knowledge about data structures What is Algorithms? ● Algorithm is a system of processes that deal with data in a programmed order by certain parameters. Abstract Data Types 1. Definition 2. Stack ADT 3. Queue ADT 1 | Definition ● Abstract Data type (ADT) is a type (or class) for objects whose behavior is defined by a set of values and a set of operations. The definition of ADT only mentions what operations are to be performed but not how these operations will be implemented. It does not specify how data will be organized in memory and what algorithms will be used for implementing the operations. It is called “abstract” because it gives an implementation-independent view. 1 | List ADT ● ● The data is generally stored in key sequence in a list which has a head structure consisting of count, pointers and address of compare function needed to compare the data in the list. The data node contains the pointer to a data structure and a self-referential pointer which points to the next node in the list. The List ADT Functions is given below: ○ get() – Return an element from the list at any given position. ○ insert() – Insert an element at any position of the list. ○ remove() – Remove the first occurrence of any element from a non-empty list. ○ removeAt() – Remove the element at a specified location from a non-empty list. ○ replace() – Replace an element at any position by another element. ○ size() – Return the number of elements in the list. ○ isEmpty() – Return true if the list is empty, otherwise return false. ○ isFull() – Return true if the list is full, otherwise return false. 1 | Stack ADT ● ● ● In Stack ADT Implementation instead of data being stored in each node, the pointer to data is stored. The program allocates memory for the data and address is passed to the stack ADT. The stack head structure also contains a pointer to top and count of number of entries currently in stack. ○ push() – Insert an element at one end of the stack called top. ○ pop() – Remove and return the element at the top of the stack, if it is not empty. ○ peek() – Return the element at the top of the stack without removing it, if the stack is not empty. ○ size() – Return the number of elements in the stack. ○ isEmpty() – Return true if the stack is empty, otherwise return false. ○ isFull() – Return true if the stack is full, otherwise return false. 1 | Queue ADT ● ● The queue abstract data type (ADT) follows the basic design of the stack abstract data type. Each node contains a void pointer to the data and the link pointer to the next element in the queue. The program’s responsibility is to allocate memory for storing the data. ○ enqueue() – Insert an element at the end of the queue. ○ dequeue() – Remove and return the first element of the queue, if the queue is not empty. ○ peek() – Return the element of the queue without removing it, if the queue is not empty. ○ size() – Return the number of elements in the queue. ○ isEmpty() – Return true if the queue is empty, otherwise return false. ○ isFull() – Return true if the queue is full, otherwise return false. II. Stack and Queue 1. Stack 2. Queue Back Next A stack ADT, a concrete data structure for a First In First out (FIFO) queue. What is Stack? A stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). Standard Stack Operations ● push(): When we insert an element in a stack then the operation is known as a push. If the stack is full then the overflow condition occurs. ● pop(): When we delete an element from the stack, the operation is known as a pop. If the stack is empty means that no element exists in the stack, this state is known as an underflow state. ● isEmpty(): It determines whether the stack is empty or not. ● isFull(): It determines whether the stack is full or not.' ● peek(): It returns the element at the given position. ● count(): It returns the total number of elements available in a stack. ● change(): It changes the element at the given position. ● display(): It prints all the elements available in the stack. 2 Queue First in First out(FIFO) 2,1. Definition 2,2. Characteristic 2,3. Menthod 2.1 | Definition ● The Queue interface is present in java.util package and extends the Collection interface is used to hold the elements about to be processed in FIFO(First In First Out) order. It is an ordered list of objects with its use limited to inserting elements at the end of the list and deleting elements from the start of the list, (i.e.), it follows the FIFO or the First-In-First-Out principle. 2.2 | Characteristic Characteristics of a Queue: The following are the characteristics of the queue: ● ● The Queue is used to insert elements at the end of the queue and removes from the beginning of the queue. It follows FIFO concept. Queue can handle multiple data. ● We can access both ends. ● They are fast and flexible. ● All Queues except the Deques supports insertion and removal at the tail and head of the queue respectively. The Deques support element insertion and removal at both ends. 2.3 | Methods Some of the commonly used methods of the Queue interface are: ● ● ● ● ● ● add() - Inserts the specified element into the queue. If the task succeeds, add() returns true, otherwise, it throws an exception. offer() - Inserts the specified element into the queue. If the task is successful, offer() returns true, otherwise, it returns false. element() - Returns the beginning of the queue. Throws an exception if the queue is empty. peek() - Returns the beginning of the queue. Returns null if the queue is empty. remove() - Returns and removes the beginning of the queue. Throws an exception if the queue is empty. poll() - Returns and removes the beginning of the queue. Returns null if the queue is empty. III. Sorting algorithms Back Next Definition A Sorting Algorithm is used to rearrange a given array or list of elements according to a comparison operator on the elements. The comparison operator is used to decide the new order of elements in the respective data structure. Complexity of Sorting Algorithms 1. 2. Time Complexity: Time complexity refers to the time taken by an algorithm to complete its execution with respect to the size of the input. Space Complexity: Space complexity refers to the total amount of memory used by the algorithm for a complete execution. It includes both the auxiliary memory and the input. Type of sorting algorithms Selection Sort Selection sort is a sorting algorithm that selects the smallest element from an unsorted list in each iteration and places that element at the beginning of the unsorted list. Selection Sort Applications The selection sort is used when ● ● ● ● a small list is to be sorted cost of swapping does not matter checking of all the elements is compulsory cost of writing to a memory matters like in flash memory Example Swap method Print array method Example selectionSort method main Example Result Insertion Sort Insertion sort is a simple sorting algorithm that works similar to the way you sort playing cards in your hands. The array is virtually split into a sorted and an unsorted part. Values from the unsorted part are picked and placed at the correct position in the sorted part. Characteristics of Insertion Sort ● ● ● This algorithm is one of the simplest algorithm with simple implementation Basically, Insertion sort is efficient for small data values Insertion sort is adaptive in nature, i.e. it is appropriate for data sets which are already partially sorted. Example insertSort method Example main Example Result IV. Two network shortest path algorithms Everything should be made as simple as possible, but not simpler (Albert Einstein) Back Next Definition Shortest path algorithms are a family of algorithms designed to solve the shortest path problem. The shortest path problem is something most people have some intuitive familiarity with: given two points, A and B, what is the shortest path between them? In computer science, however, the shortest path problem can take different forms and so different algorithms are needed to be able to solve them all. Types of shortest path algorithms There are two main types of shortest-path algorithms: Single-source: Given a graph G, with vertices V, edges E with weight function w(u,v)=wu,v , and a single source vertex, s, return the shortest paths from s to all other vertices in V. All-pairs: Given a graph G, with vertices V, edges E with weight function w(u,v)=wu,v return the shortest path from u to v for all (u,v) in V. Algorithms Bellman-Ford algorithm: The Bellman-Ford algorithm solves the single-source problem in the general case, where edges can have negative weights and the graph is directed. If the graph is undirected, it will have to modify by including two edges in each direction to make it directed. Floyd-Warshall algorithm: Floyd-Warshall takes advantage of the following observation: the shortest path from A to C is either the shortest path from A to B plus the shortest path from B to C or it's the shortest path from A to C that's already been found. This may seem trivial, but it's what allows Floyd-Warshall to build shortest paths from smaller shortest paths, in the classic dynamic programming way. Reference • Software Stack - Definition & Overview - https://www.sumologic.com/glossary/software-stack/ • What is Software Stack - https://www.geeksforgeeks.org/what-is-software-stack/ • Implementing Stacks in Data Structures https://www.simplilearn.com/tutorials/data-structure-tutorial/stacks-in-data-structures • Stack - https://www.techopedia.com/definition/9523/stack • Stack Memory - https://www.sciencedirect.com/topics/engineering/stack-memory • How does memory stacks work in Javascript? https://www.geeksforgeeks.org/how-does-memory-stacks-work-in-javascript/ • Introduction to memory and memory units https://www.geeksforgeeks.org/introduction-to-memory-and-memory-units/?ref=rp • What is a non-linear data structure? https://www.javatpoint.com/what-is-a-non-linear-data-structure • What is Linked List - https://www.geeksforgeeks.org/what-is-linked-list/ • Queue Data Structure - https://www.geeksforgeeks.org/queue-data-structure/