Docsity

Assignment Data Structures and Algorithms, Assignments of Data Structures and Algorithms

Toggle sidebar
bg1
PROGRAM TITLE: COMPUTING IN SOFTWARE ENGINEERING
UNIT TITLE: UNIT 19: DATA STRUCTURE AND ALGORITHM
ASSIGNMENT NUMBER: …………………1………………………….......
ASSIGNMENT NAME:......OPTIMUM CARGO SHIP ROUTING (OASR)
SUBMISSION DATE: ……………………………………........................
DATE RECEIVED: ……………………………………………....................
TUTORIAL LECTURER: ………Nguyen Ngoc Tan …………......... .
WORD COUNT: ………………………………………………………...........
STUDENT NAME: ………Do Long Nhat…………………………….
STUDENT ID: ………………BKC12174………………………………
MOBILE NUMBER: …………0901569964………………………
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25

Partial preview of the text

Download Assignment Data Structures and Algorithms and more Assignments Data Structures and Algorithms in PDF only on Docsity!

PROGRAM TITLE: COMPUTING IN SOFTWARE ENGINEERING

UNIT TITLE: UNIT 19: DATA STRUCTURE AND ALGORITHM

ASSIGNMENT NUMBER: …………………1………………………….......

ASSIGNMENT NAME:......OPTIMUM CARGO SHIP ROUTING (OASR)

SUBMISSION DATE: ……………………………………........................

DATE RECEIVED: ……………………………………………....................

TUTORIAL LECTURER: ………Nguyen Ngoc Tan ………….......... WORD COUNT: ………………………………………………………........... STUDENT NAME: ………Do Long Nhat……………………………. STUDENT ID: ………………BKC12174……………………………… MOBILE NUMBER: …………0901569964………………………

Summative Feedback:

I. Introduction

I work as in-house software developer for BKC Software Company, a software company providing network provisioning solutions. My company is part of a collaborative service provisioning development project and my company has won the contract to design and develop a "Optimum Cargo Ship Routing (OASR)" solution. My account manager has assigned me a special role that is to inform my team about designing and implementing abstract data types. My have been asked to create a report for all collaborating partners on how ADTs can be utilized to improve software design, development and testing and how to specify abstract data types and algorithms in a formal notation, how to evaluate the effectiveness of data structures and algorithms.

II. Theoretical basis

1. Data Structure and Algorithm ● Data Structure A data structure is a storage that is used to store and organize data. It is a way of arranging data on a computer so that it can be accessed and updated efficiently. The importance of Data Structure 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. Data structures bring together the data elements in a logical way and facilitate the effective use, persistence and sharing of data. They provide a formal model that describes the way the data elements are organized. Data structures are the building blocks for more sophisticated applications. They are designed by composing data elements into a logical unit representing an abstract data type that has relevance to the algorithm or application. There are 2 types of Data Structure :  Primitive Data Structure  Non – Primitive Data Structure

Figure 2: Classify the Non-Primitive Data Structures Simple data structure Simple data structure build on data types to create higher level data structures. The most common simple data structures are Array and Linked lists. Array The array data structure is one of the oldest and most popular data structures. An array of elements can be values or variables. The structure determines the elements using an index or key, allowing the data structure to calculate the position of each element. The memory address of the original element is called the first platform or address. Data elements are indexed and stored sequentially in adjacent memory. There are different types of array data structures. In a common example, many databases use one-dimensional linear arrays that have database records. Arrays can also be multidimensional if they access elements from more than one index. Arrays are the fundamental structure for many other data structures including hash tables, queues, stacks, and linked lists.

Linked List Link list is the second most popular data structure type. It links elements instead of computer addresses from pointers. While an array of mathematically computes data entries, linked lists store data items in its own structure. The structure considers each element to be a single object and each object contains data and references or the address of the next element. There are three types of linked lists: singly linked where each node stores the next node’s address and the end address is null, doubly linked where each node stores the previous and next node’s addresses and the end address is null, and circular linked where each node links to the other in a circle, and there is no ending null. Compound data structure Computing systems combine simple data structures to form compound data structures. Compound structures may be linear or non-linear. a. Linear Linear data structure from sequences Stack A stack is a basic linear data structure: a logical entity visualized as a physical stack or object stack. The data structure inserts and deletes elements at one end of the stack, called vertices. Developers develop a stack using arrays and linked lists. A stack in the order in which the computer system performs operations. The order is usually the first appearance (LIFO). The main activities for the stack include 1) Push, add an item to the stack; and 2) Pop, remove an item from the stack in reverse order to Push. Stack also returns an isEmpty value: it means true on an empty stack and deletes false if there is data. Queue Instead of the stack LIFO order, the queue data structure places elements in the queue in the order of First In First Out (FIFO). The insertion process is called Enqueue, which inserts an element into the back or tail of the queue. The delete procedure is called Dequeue, removing elements from the front or the beginning of the queue. To move the element that is inserted into the Dequeued queue, the stack data structure must remove all elements between the new element and the front of the queue.

Figure 3. The example of tree Trees are hierarchical data structures that help organizations store data. The purpose of the tree is to store natural hierarchical information, such as the file system. There are many types of plants. A binary tree is a tree data structure in which each node has no more than two children, respectively, is called a right child and a left child. Additional structures are binary trees, often used to effectively store router tables for highbandwidth routers; or Merkel tree, the hash value of each subtree in its parent node. Merkel allows databases like NoQuery Apache Cassandra to effectively verify large data content. ● Algorithm An algorithm is defined as a step-by-step procedure or method for solving a problem by a computer in a finite number of steps. Steps of an algorithm definition may include branching or repetition depending upon what problem the algorithm is being developed for. Types of Algorithm: Brute Force Algorithm: This is the most basic and simplest type of algorithm. A Brute Force Algorithm is the straightforward approach to a problem i.e., the first approach that comes to our mind on seeing the problem. More technically it is just like iterating every possibility available to solve that problem. Recursive Algorithm:

This type of algorithm is based on recursion. In recursion, a problem is solved by breaking it into subproblems of the same type and calling own self again and again until the problem is solved with the help of a base condition. Some common problem that is solved using recursive algorithms are Factorial of a Number, Fibonacci Series, Tower of Hanoi, DFS for Graph, etc. a) Divide and Conquer Algorithm: In Divide and Conquer algorithms, the idea is to solve the problem in two sections, the first section divides the problem into subproblems of the same type. The second section is to solve the smaller problem independently and then add the combined result to produce the final answer to the problem. Some common problem that is solved using Divide and Conquers Algorithms are Binary Search, Merge Sort, Quick Sort, Strassen’s Matrix Multiplication, etc. b) Dynamic Programming Algorithms: This type of algorithm is also known as the memoization technique because in this the idea is to store the previously calculated result to avoid calculating it again and again. In Dynamic Programming, divide the complex problem into smaller overlapping subproblems and store the result for future use. The following problems can be solved using the Dynamic Programming algorithm Knapsack Problem, Weighted Job Scheduling, Floyd Warshall Algorithm, etc. c) Greedy Algorithm: In the Greedy Algorithm, the solution is built part by part. The decision to choose the next part is done on the basis that it gives an immediate benefit. It never considers the choices that had been taken previously. Some common problems that can be solved through the Greedy Algorithm are Dijkstra Shortest Path Algorithm, Prim’s Algorithm, Kruskal’s Algorithm, Huffman Coding, etc. d) Backtracking Algorithm: In Backtracking Algorithm, the problem is solved in an incremental way i.e. it is an algorithmic technique for solving problems recursively by trying to build a solution incrementally, one piece at a time, removing those solutions that fail to satisfy the constraints of the problem at any point of time.

 Encapsulation allows access to a level without revealing the complex details below that level.  It reduces human errors.  Simplifies the maintenance of the application  Makes the application easier to understand. ●Queue A queue is a linear data structure that stores the elements sequentially. It uses the FIFO approach (First In First Out) for accessing elements. Queues are typically used to manage threads in multithreading and implementing priority queuing systems. Types of Queues in Data Structure There are four different types of queues in data structures:  Simple Queue  Circular Queue  Priority Queue  Double-Ended Queue (Deque)

Simple Queue

It is the most basic queue in which the insertion of an item is done at the front of the queue and deletion takes place at the end of the queue.  Ordered collection of comparable data kinds.  Queue structure is FIFO (First in, First Out).  When a new element is added, all elements added before the new element must be deleted in order to remove the new element. Figure 4: Simple Queue Circular Queue A circular queue is a special case of a simple queue in which the last member is linked to the first. As a result, a circle-like structure is formed.  The last node is connected to the first node.

 Also known as a Ring Buffer as the nodes are connected end to end.  Insertion takes place at the front of the queue and deletion at the end of the queue. Application of circular queue: Insertion of days in a week. Figure 5 : Application of circular queue Now let’s take a look at the priority queue in the data structure.

Priority Queue

In a priority queue, the nodes will have some predefined priority in the priority queue. The node with the least priority will be the first to be removed from the queue. Insertion takes place in the order of arrival of the nodes. Some of the applications of priority queue:  Dijkstra’s shortest path algorithm  Prim’s algorithm  Data compression techniques like Huffman code Below diagram shows how an application use priority queue for the items consumed by the user.

Basic Operations: − Queue operations may involve initializing or defining the queue, utilizing it, and then completely − erasing it from the memory. Here we shall try to understand the basic operations associated with queues

  • enqueue() − add (store) an item to the queue.
  • dequeue() − remove (access) an item from the queue. Few more functions are required to make the above-mentioned queue operation efficient. These are
  • peek() − Gets the element at the front of the queue without removing it.
  • isfull() − Checks if the queue is full.
  • isempty() − Checks if the queue is empty. − In queue, we always dequeue (or access) data, pointed by front pointer and while enqueing (or storing) data in the queue we take help of rear pointer. Applications of Queue: − Queue is used when things don’t have to be processed immediately, but have to be processed in First In First Out order like Breadth First Search. This property of Queue makes it also useful in following kind of scenarios. -When a resource is shared among multiple consumers. Examples include CPU scheduling, Disk Scheduling. When data is transferred asynchronously (data not necessarily received at same rate as sent) between two processes. Examples include IO Buffers, pipes, file IO, etc. ● Stack Stack is a linear data structure which 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). A stack can be implemented in a random access memory (RAM) attached to a CPU. The implementation of a stack in the CPU is done by assigning a portion of memory to a stack operation and using a processor register as a stack pointer. The starting memory location of the stack is specified by the processor register as stack pointer. Operations:

− A Stack contains elements of the same type arranged in sequential order. All operations take place at a single end that is top of the stack and following operations can be performed:

  • 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. Exception Operations pop and top cannot be performed if the stack is empty
    • Attempting the execution of pop or top on an empty stack should throws a StackEmptyException − Operations push sometimes cannot be performed if it’s not enough memory
    • Attempting the execution of push when there is not enough memory should throws a OutOfMemoryError II. Application: − Any sort of nesting (such as parentheses)
  • Evaluating arithmetic expressions (and other sorts of expression)
  • Implementing function or method calls
  • Keeping track of previous choices (as in backtracking)
  • Keeping track of choices yet to be made (as in creating a maze)
  • Undo sequence in a text editor
  • Auxiliary data structure for algorithms
  • Component of other data structure Method calls and the implementation by using stack − Each time a method is called, an activation record (AR) is allocated for it. − This record contains the following information:
  • Parameters and local variables used in the called method
  • Dynamic link: a pointer to the caller’s activation record

Output: Proving examples, using illustrations to analyse two network shortest path algorithm. I. Dijkstra's algorithm: − Dijkstra's algorithm has many variants but the most common one is to find the shortest paths from the source vertex to all other vertices in the graph. − Algorithm Steps: Step 1. Set all vertices distances = infinity except for the source vertex, set the source distance = 0. Step 2. Push the source vertex in a min-priority queue in the form (distance, vertex), as the comparison in the min-priority queue will be according to vertices distances. Step 3. Pop the vertex with the minimum distance from the priority queue (at first the popped vertex = source). Step 4. Update the distances of the connected vertices to the popped vertex in case of "current vertex distance + edge weight < next vertex distance", then push the vertex Step 5. with the new distance to the priority queue. Step 6. If the popped vertex is visited before, just continue without using it. Step 7. Apply the same algorithm again until the priority queue is empty. − Example: Step 1. Start with a weighted graph

Step 2. Choose a starting vertex and assign infinity path values to all other devices Step 3. Go to each vertex and update its path length