Docsity

Assignment 1 Data Structure and Algorithm, Assignments of Data Structures and Algorithms

Toggle sidebar
bg1
ASSIGNMENT 1 DATA STRUCTURE AND ALGORITHM
Qualification
BTEC Level 5 HND Diploma in Computing
Unit number and title
Unit 19: Data Structures and Algorithms
Submission date
Date Received 1st
submission
Re-submission Date
Date Received 2nd
submission
Student Name
Phan Minh Tiến
Student ID
GCD201914
Class
GCD1001
Assessor name
Hồ Văn Phi
Student declaration
I certify that the assignment submission is entirely my own work and I fully understand the consequences of plagiarism. I understand that
making a false declaration is a form of malpractice.
Student’s signature
Tiến
Grading grid
P2
P3
M1
M2
M3
D1
D2
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
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36

Partial preview of the text

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

ASSIGNMENT 1 DATA STRUCTURE AND ALGORITHM

Qualification BTEC Level 5 HND Diploma in Computing

Unit number and title Unit 19 : Data Structures and Algorithms

Submission date Date Received 1st submission

Re-submission Date Date Received 2nd submission

Student Name Phan Minh Tiến Student ID GCD

Class GCD1001^ Assessor name Hồ^ Văn Phi

Student declaration

I certify that the assignment submission is entirely my own work and I fully understand the consequences of plagiarism. I understand that making a false declaration is a form of malpractice.

Student’s signature Tiến

Grading grid

P1 P2 P3 M1 M2 M3 D1 D

 Summative Feedback:  Resubmission Feedback:

Grade: Assessor Signature: Date:

Internal Verifier’s Comments:

IV Signature:

  • LO1. Examine abstract data types, concrete data structures and algorithms
  • structures........................................................................................................................................................................ P1. Create a design specification for data structures explaining the valid operations that can be carried out on the
    • I. Abstract Data Type (ADT)
  • P2. Determine the operations of a memory stack and how it is used to implement calls in a computer.
    • I. What is memory stack
    • II. Operations
    • III. Exception
    • IV. Application
    • V. Method calls and the implementation by using stack
    • VI. Implement Stack by Array in Java.
  • M1. Illustrate, with an example, a concrete data structure for a First In First Out (FIFO) queue................................
    • I. Definition of Queue
    • II. Queue Representation
    • III. Basic Operations
    • IV. Applications of Queue
    • V. Array implementation of Queue in Java
    • VI. Car driver using Queue
  • M2. Compare the performance of two sorting algorithms.
    • I. Selection Sort.........................................................................................................................................................
    • II. Bubble Sort
  • LO2. Specify abstract data types and algorithms in a formal notation.(P3)
  • P3 Using an imperative definition, specify the abstract data type for a software stack.
    • I. Definition of software stack
    • II. Parts of a software stack
    • III. Five Software Stack Examples
    • IV. The problem specific use Data Structure
  • M3. Examine the advantages of encapsulation and information hiding when using an ADT
  • each. D1. Analyze the operation, using illustrations, of two network shortest path algorithms, providing an example of
    • I. Dijkstra’s algorithm................................................................................................................................................
    • II. Bellman Ford’s Algorithm
  • you agree. D2. Discuss the view that imperative ADTs are a basic for object orientation and, with justification, state whether
  • REFERENCES
  • Figure 1: ADT Table of Figures
  • Figure 2: Class Array
  • Figure 3: Main method
  • Figure 4: Class LinkedList
  • Figure 5: Main method
  • Figure 6: Principle of Stack
  • Figure 7: Class Stack
  • Figure 8: Queue
  • Figure 9: Class Queue
  • Figure 10: Class Car.......................................................................................................................................................
  • Figure 11: Class QueueForCars
  • Figure 12: Class Test
  • Figure 13: Class Student
  • Figure 14: Class ListStudent
  • Figure 15: Class Main....................................................................................................................................................
  • Figure 16: Example use Encapsulation
  • Figure 17: Class Node
  • Figure 18: Class Dijkstra................................................................................................................................................
  • Figure 19: Main method
  • Figure 20: Class Bellman

LO1. Examine abstract data types, concrete data structures and

algorithms

P1. Create a design specification for data structures explaining

the valid operations that can be carried out on the structures.

I. Abstract Data Type (ADT)

Abstract data type  Abstract Data type (ADT) is a type (or class) for objects whose behavior is defined by a set of value 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. The process of providing only the essentials and hiding the details is known as abstraction.

Figure 1: ADT

 The user of data type does not need to know how that data type is implemented. For example, we have been using Primitive values like int, float, char data types only with the knowledge that these data type can operate and be performed on without any idea of how they are implemented. So, a user only needs to know what a data type can do, but not how it will be implemented. Think of ADT as a black box which hides the inner structure and design of the data type. Now we’ll define three ADTs namely List ADT, Stack ADT, Queue ADT.  Example of ADT Array of ADT:

Figure 2: Class Array

Main method:

Figure 3: Main method

Figure 4: Class LinkedList

P2. Determine the operations of a memory stack and how it is

used to implement calls in a computer.

I. What is memory stack 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.

II. 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. It works on principle LIFO (Last In First Out). Moreover, the stack 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 the 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.

Figure 6: Principle of Stack

III. 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 StackEmtptyExceptiom. 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.

IV. 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

V. 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  Return address to resume control by the caller (address of instruction immediately following the call)  Return value for a method not declared as void  Since the size of AR may vary from one call to another, returned value is placed right above the AR of the caller.  Each new AR is placed on top of the run-time stack  When a method terminates, its AR is removed from the top of the run-time stack. Thus, the first AR placed on the stack is the last one removed.

VI. Implement Stack by Array in Java.

Class Stack:

M1. Illustrate, with an example, a concrete data structure for a

First In First Out (FIFO) queue.

I. Definition of Queue

  • Like Stack, Queue is a linear structure which follows a particular order in which the operations are performed. The order is First In First Out (FIFO). A good example of queue is any queue of consumers for a resource where the consumer that came first is served first.
  • The difference between stacks and queues is in removing. In a stack we remove the item the most recently added. In a queue, we remove the item the least recently added.

II. Queue Representation

  • As we now understand that in queue, we access both ends for different reasons. The following image given below tries to explain queue representation as data structure:

Figure 8: Queue

  • As in stacks, a queue can also be implemented using Arrays, Linked-lists, Pointers and Structures. For the sake of simplicity, we shall implement queues using one-dimensional array.

III. 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 function 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 dequeuer(or access) data, pointed by front pointer and while enquiring (or storing) data in the queue we take help of rear pointer.

IV. 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.

V. Array implementation of Queue in Java

Class Queue

Figure 10: Class Car

Class QueueForCars: