1. Trang chủ
  2. » Luận Văn - Báo Cáo

program title btec in computing software engineering unit title data structure and algorithm optimum cargo ship routing oasr

31 0 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Nội dung

I have been asked tocreate a report for all collaboratingpartners on how ADTs can be utilized to improve software design, development and testingand how to specify abstract data types an

Trang 1

PROGRAM TITLE: BTEC IN COMPUTING (SOFTWARE ENGINEERING)UNIT TITLE: DATA STRUCTURE AND ALGORITHM

ASSIGNMENT NUMBER: ASSIGNMENT 01

ASSIGNMENT NAME: OPTIMUM CARGO SHIP ROUTING (OASR)SUBMISSION DATE:

Trang 4

II THEORETICAL BASIS 8

1 Data Structure and Algorithm 8

2 Design Specification 21

3 Error handling 22

4 The effectiveness of data structures and algorithms 22

III IMPLEMENTATION 26

Trang 5

Figure 1 Array Example 9

Figure 2 Stack Example 9

Figure 3 Queue Example 10

Figure 4 Linked List Example 10

Figure 5 Binary Search Tree Example 10

Figure 6 The working of Queue 14

Figure 7 Working on Stack 16

Figure 8 Console Interface 18

Figure 9 Big-O Graph Example 24

Trang 6

Table 1 Organization of SDL 22Table 2 Compare performance of Bubble Sort and Selection Sort Algorithm 24Table 3 Example code of more time, less space and less time, more space 26

Trang 7

LIFO Last-in First-out

Trang 8

In this report, I work as in-house software developer for BKC Software Company, a softwarecompany providing network provisioning solutions My company is part of a collaborativeservice provisioning development project and your company has won the contract to designand develop a "Optimum Cargo Ship Routing (OASR)" solution.

My account manager has assigned me a special role that is to inform my team about designingand implementing abstract data types I have been asked tocreate a report for all collaboratingpartners on how ADTs can be utilized to improve software design, development and testingand how to specify abstract data types and algorithms in a formal notation, how to evaluatethe effectiveness of data structuresand algorithms.

1 Data Structure and Algorithm

What is Data Structure?

A data structure is a specialized format for organizing, processing, retrieving, and storing data.There are several basic and advanced types of data structures, each designed to organize datafor a specific purpose Data structures make it easier for users to access and manipulate thedata they need Most importantly, data structures frame the organization of information so thatit can be better understood by machines and humans.

The importance of Data Structure.

In computer science, the importance of data structures is ubiquitous Data structures providethe basics for solving problems Its meaning can be understood as follows.

1) The software consists of her two parts: front-end and back-end The front end providesthe interface and the back end is called the database containing customer records Therecan be millions or trillions of customers When you need to find records for a particularcustomer or many customers, you do so using search methods, which are operations ondata structures.

2) When the software is executed, it is first inserted into the computer's memory Jobs arequeued in computer memory A queue is also a data structure concept.

3) As you know, a queue is formed when jobs and processes are entered These queues maycontain too many jobs or processes In the queue, jobs are processed in the order they are

Trang 9

entered When a job is created, it is placed at the end of the queue Assuming you need toadd or remove jobs in any order, the queue concept fails and the linked list concept isused.

4) When storing data hierarchically, the concept of a tree is used.

5) If the data is ordered alphabetically or numerically, it is done by the sorting operation,which is an operation on the data structure.

Types of Data Structure and Example.

Linear data structure

1) Array

Arrays store a collection of elements in contiguous locations Elements of the same type arestored together so that the position of each element can be calculated or easily retrieved viaindex Array lengths can be fixed or flexible.

Figure 1 Array Example.

2) Stack

A stack is a collection of data items following the last-in, first-out (LIFO) rule New itemsadded to the stack are above all previously added items Only the top element of the stack canbe removed Stacks are ideal for retrieving recently used objects or data in the order they wereentered.

Trang 10

Figure 2 Stack Example.

3) Queue

A queue is a collection of data items that follow a first-in, first-out (FIFO) rule Unlike stacks,queues place new items at the end of all previously added items The first item in the queue isalways removed first.

Figure 3 Queue Example.

4) Linked List

A linked list stores a collection of items in linear order Each item or node in a linked listcontains a data item and a reference or link to the next item in the list.

Figure 4 Linked List Example.

Non-linear data structure

5) Binary Tree

Trang 11

A tree stores a collection of items in an abstract, hierarchical fashion Each node has a keyvalue associated with it, and parent nodes are linked to child nodes (subnodes) There is a rootnode that is the ancestor of all nodes in the tree.

Figure 5 Binary Search Tree Example.

6) Graph:

A graph is a non-linear data structure consisting of vertices (or nodes) and edges It consists ofa finite set of vertices and a set of edges connecting pairs of vertices Graphs are used to solvethe most demanding and complex programming problems There are various terms such as path, degree, adjacent vertices, connected components.

What is Algorithm?

An algorithm is defined as a step-by-step procedure or method for solving a problem by acomputer in a finite number of steps The algorithm definition procedure can branch or repeatdepending on the problem for which the algorithm is being developed When defining analgorithm, the steps are written in a human-readable language and are programming languageindependent It can be implemented in any programming language.

The importance of Algorithm.

Algorithms are important for understanding the relationship between how data is processedand how computers interpret it Algorithms help optimize code, including the advantageoususe of mathematical techniques, making software more efficient.

As the complexity of computer algorithms increases, so does data usage, which can impactapplication performance and introduce several problem areas.

Processing speed:

Trang 12

High-speed processing is necessary to process very large amounts of data, but when theamount of data processed increases, the necessary processing speed may not be obtained.

Backtracking Algorithm

A backtracking algorithm is a way to solve a problem in an additional way It uses therecursive approach, or we can say that the recursive algorithm to explain and understand theproblem.

Trang 13

So, in simple terms, we can say that Backtracking is a systematic and additional way of tryingdifferent sequences of decisions for searching a solution until we found a one that works.

Divide and conquer

Divide and conquer also one of the important algorithms This algorithm has two parts firstand then divides the problems into smaller subproblems or we can say divide those two partsfurther And solve those subproblems or parts recursively and at last combine all thosesubproblems solution to form the solution of the original problem.

Example of Divide and Conquer Algorithm : Merge Sort and Quick Sort.Greedy algorithm

The greedy algorithm is the algorithm that looking for optimal choice at each step because itis looking for finding the overall optimal solution for the entire problem.

This algorithm is generally used for optimizing the result But it is not necessary that we canfind an optimal solution by using this algorithm.

What is Abstract Data Type (ADT)?

An abstract data type (ADT) is a concept or model of a data type Thanks to ADT, the userdoesn't have to worry about how the data type was implemented In addition, ADT alsohandles the implementation of functions for data types Here, users have predefined functionsfor each data type, ready to use for any operation.

ADT is a common and important data type In general, ADTs are mathematical or logicalconcepts that can be implemented on different machines in different languages It is also veryflexible and language and machine independent.

The advantages of encapsulation and information hiding when using an ADT.Encapsulation means grouping software into easy-to-use units with well-defined and simpleinterfaces Encapsulation and information hiding are related, as both aim to simplify the useof classes The way to make classes user-friendly is to hide implementation details Thismakes the user-facing interface easier to understand.

Hiding details from users of your class can be done using scopes If you have public scopevariables and functions, everything outside the class can access them If you want to hidevariables and functions, make them private scope instead Typically, when implementing

Trang 14

encapsulation, you make all property variables private and define public methods for settingand getting values, called accessor and mutator methods.

Advantages of using encapsulation and information hiding

Encapsulation allows objects to operate independently because the data is hidden.The information hidden in an object can only be accessed through its methods, thusforming a kind of interface information that matches the object's outer world.

Objects have an interface that they use to communicate and their data is protected fromthe outside.

You can replace the whole object with a new one because the interface is the same.Small changes in object behavior and state can cause system damage if encapsulation isnot done properly To fix errors, encapsulation also helps a lot, as the entire systemremains intact and only the infected object is repaired.

Encapsulation is very useful when rewriting (adding) or changing parts of a programwithout having to edit the entire program In other words, code optimization is onlypossible through encapsulation.

Encapsulation helps transform the outside while protecting the internal details of theobject.

Object encapsulation is like a chain of information boxes, and if one box breaks, it canbe repaired or replaced without disturbing the inner material of the others.

Trang 15

IsFull: Check if the queue is full

Peek: Get the value of the front of the queue without removing itQueue operations work as follows:

two pointers FRONT and REARFRONT track the first element of the queueREAR track the last element of the queueinitially, set value of FRONT and REAR to -1Enqueue Operation

check if the queue is full

for the first element, set the value of FRONT to 0increase the REAR index by 1

add the new element in the position pointed to by REARDequeue Operation

check if the queue is emptyreturn the value pointed by FRONTincrease the FRONT index by 1

for the last element, reset the values of FRONT and REAR to -1

Trang 16

Figure 6 The working of Queue.What is Stack?

A stack is a linear data structure that follows the last-in-first-out (LIFO) principle This meansthat the last item added to the stack is removed first.

A stack data structure can be thought of as a stack of disks on top of each other.Here you can:

Put a new plate on topRemove top panel

If you want the bottom slab, you must first remove all the top slabs This is exactly how thestack data structure works.

The operation of Stack

There are some basic operations that allow us to perform different actions on a stack.Push: Add an element to the top of a stack

Pop: Remove an element from the top of a stack

Trang 17

IsEmpty: Check if the stack is emptyIsFull: Check if the stack is full

Peek: Get the value of the top element without removing itThis is how it is used to implement function callsThe operations work as follows:

A pointer called TOP is used to keep track of the top element in the stack.

When initializing the stack, we set its value to -1 so that we can check if the stack isempty by comparing TOP == -1.

On pushing an element, we increase the value of TOP and place the new element in theposition pointed to by TOP.

On popping an element, we return the element pointed to by TOP and reduce its value.Before pushing, we check if the stack is already full.

Before popping, we check if the stack is already empty.

Figure 7 Working on Stack.Illustrate with an example for First in First out (FIFO) queue

I create class Queue to implement some basic operations that allow us to perform differentactions on a queue

package Assignment;

Trang 18

int front rear max_size, , ; int[] arr;

public Queue(int size){ arr = new int[size]; front = -1; rear = -1; max_size = size; }

public boolean isFull(){

if(front == && 0 rear == max_size - ){1 return true;

else } { return false; } }

public boolean isEmpty(){ if(front == -1){ return true; else } { return false; } }

public void enQueue(int element){ if(isFull()){

System.out.println("Queue is full!") else } {

if(front == -1){ front = 0; }

arr[++rear] = element; }

}

public int deQueue() { int element; if(isEmpty()){

System.out.println("Queue is empty") return ;-1

Trang 19

if(front > rear){ front rear = - 1; }else{ front++; }

return element; }

} }}

Next, I create class Main.java to implement the example.

package Assignment;

import java.util.Scanner;

public class Main {

public static void main (String[] args){ Scanner sc = new Scanner(System )in; Queue queue = new Queue( )10; System.out.println("Please enter n: "); int n = Integer.parseInt(sc.nextLine()); System.out.println("Please enter k: "); int k = Integer.parseInt(sc.nextLine()); sc.close();

for int ( i =0; i < n i++){;

Trang 20

queue.display();

System.out.println(" -"\n ); for int ( i=0; i < k i++){; int x = queue.deQueue(); queue.enQueue(x); }

queue.display(); }

Dijkstra's algorithm can find the shortest paths from the source node to all nodes in theweighted graph The shortest path is also at the source node of the graph.

Finding the shortest path by Dijkstra's algorithm produces a shortest path tree (SPT) with aroot source point.

When implementing Dijkstra's algorithm in Java, we maintain two lists or sets The firstcontains all the vertices of the shortest path tree and the second contains the evaluation phasevertices for inclusion in the SPT.

At each iteration find a node from the second list with the shortest path The step-by-stepprocess of Dijkstra's algorithm is as follows:

Trang 21

1) First, mark all nodes in the chart as unvisited.

2) Now initialize the start node with zero All other nodes with infinity imply themaximum number.

3) Make the starting node the current node.

4) Using this current node, we analyze all unvisited adjacent nodes, add edge weights tocompute distances, and create connections between the current node and adjacent nodes.5) Compare the recently calculated distances to the distances assigned to neighboring

nodes This is treated as the current distance of the neighboring nodes.

6) Now look at the not-yet-visited nodes around the current node and mark the currentnode as visited.

7) This process repeats until the terminal node is marked as visited This means thatDijkstra's algorithm has finished its task If the terminal node is not yet marked asvisited:

8) Selecting an unvisited node with the shortest path makes it the new current node Thenrepeat the process from step 4.

Psuedo Code for Dijkstra’s Algorithm

Method DIJKSTRA(G, SV) G-> graph;

SV->starting vertex;begin

for every vertex VX in G //initialization; set the initial path toinfinite and current node to 0 or null;

Distance[VX] <- infinite Current[VX] <- NULL

If V != SV, add VX to Priority Queue // During the first run, this vertex is the source or starting node

Distance[SV] <- 0

Ngày đăng: 13/05/2024, 15:06

w