ET 2006 DATA STRUCTURES ALGORITHMS Lecture 1 Introduction

  • Slides: 51
Download presentation
ET 2006 : DATA STRUCTURES & ALGORITHMS Lecture 1: Introduction Malaka J Walpola

ET 2006 : DATA STRUCTURES & ALGORITHMS Lecture 1: Introduction Malaka J Walpola

OUTLINE - I � Class Details Learning Outcomes & Course Outline � Assessments �

OUTLINE - I � Class Details Learning Outcomes & Course Outline � Assessments � Class Rules & Expectations � � � Discussion & Survey Introduction to Algorithms What is an algorithm? � Flowcharts � Pseudocode � 2

OUTLINE - II � Basic Data Structures Introduction to Data Structures and Abstract Data

OUTLINE - II � Basic Data Structures Introduction to Data Structures and Abstract Data Types � Basic Data Structures & Operations on Them � Arrays � Records � Lists � Stack � Queue � Dictionary � 3

CLASS DETAILS 4

CLASS DETAILS 4

ET 2006: DATA STRUCTURES & ALGORITHMS � � � Credits: 4 (GPA) Pre-requisites :

ET 2006: DATA STRUCTURES & ALGORITHMS � � � Credits: 4 (GPA) Pre-requisites : ET 1001 -Introduction to ICT Course Objective: � To provide knowledge of basic data structures and algorithmic design paradigms used to manipulate data structures for common functions in software programming 5

CS 2022 : DATA STRUCTURES & ALGORITHMS � Lecturers: Malaka J. Walpola � Contact

CS 2022 : DATA STRUCTURES & ALGORITHMS � Lecturers: Malaka J. Walpola � Contact information E-mail: malaka@cse. mrt. ac. lk � Phone: 0718661380 � 6

CS 2022 : DATA STRUCTURES & ALGORITHMS � Hours: � Lectures – (45 Hrs:

CS 2022 : DATA STRUCTURES & ALGORITHMS � Hours: � Lectures – (45 Hrs: 4. 5 to 5 Hrs/week) � Monday 1. 00 – 5. 30/6. 00 Labs/Assignments (30 Hrs) � Reading the Books, Self Study & Homework (will depend on individuals) � 7

LEARNING OUTCOMES � On the completion of this module, the student will be able

LEARNING OUTCOMES � On the completion of this module, the student will be able to: LO 1: Select and implement common data structures for a given situation [12 Hrs] � LO 2: Implement appropriate algorithms to manipulate data structures [12 Hrs] � LO 3: Analyze the performance and complexity of basic algorithms [9 Hrs] � LO 4: Use data structures and synthesize efficient algorithms to solve real life problems [12 Hrs] � 8

COURSE OUTLINE - I � Select and implement common data structures for a given

COURSE OUTLINE - I � Select and implement common data structures for a given situation [12] � Basic data structures and their properties: linear (arrays, linked-lists, stacks, queues) � Non-linear (trees and graphs) � Operations on data structures: insertion, deletion, sorting, searching, traversing � Heap abstract data type and heap operations; � Dictionaries and hash tables � 9

COURSE OUTLINE - II � Implement appropriate algorithms to manipulate data structures [12] �

COURSE OUTLINE - II � Implement appropriate algorithms to manipulate data structures [12] � � Characteristics of an algorithm. Algorithm creation techniques and matrices of algorithms: � � � Algorithms for stacks and queues: first in first out (FIFO) and last in first out(LIFO) Sorting algorithms: � � Recursion: tail and non-tail, direct and indirect; Internal and external sorting Simple Sorting Algorithms (Bubble, selection, insertion) Advanced Sorting Algorithms (Merge, Quick, Bucket, Shell, Heap) Searching Algorithms: - linear (sequential) search and binary search (BST) 10

COURSE OUTLINE - III � Analyze the performance and complexity of basic algorithms [9]

COURSE OUTLINE - III � Analyze the performance and complexity of basic algorithms [9] Factors considered in analyzing algorithms, and growth of functions � Asymptotic notations � Time complexity and asymptotic complexity analysis � Efficiency of different searching and sorting algorithms � 11

COURSE OUTLINE - IV � Use data structures and synthesize efficient algorithms to solve

COURSE OUTLINE - IV � Use data structures and synthesize efficient algorithms to solve real life problems [12] Solving real-life recursion problems: factorials, Fibonacci numbers, Towers of Hanoi. � Divide and conquer techniques; Tree and graph traversal techniques (Death-first and Breadth first), Handling cycles and vertices, Tree traversal preorder, in-order and post-order � Graph traversal algorithms: - Bellman-Ford shortest-path and Dijkstra's shortest-path algorithms � 12

METHOD OF ASSESSMENT � Exam – 70% 3 hour � Closed book � Answer

METHOD OF ASSESSMENT � Exam – 70% 3 hour � Closed book � Answer all � Will have short answer/multiple choice questions � � Assignments – 30% Assignment 1: On LO 1 & LO 2 - 10% � Assignment 2: On LO 3 - 10% � Assignment 3: On LO 4 - 10% � 13

RECOMMENDED TEXT BOOKS � � � Adam Drozdek, Data Structures and algorithms in Java,

RECOMMENDED TEXT BOOKS � � � Adam Drozdek, Data Structures and algorithms in Java, , 3 rd Ed. , Cengage Learning Asia, 2008 Michael Goodrich and Roberto Tamassia, Data Structures and Algorithms in Java; 5 th Ed. , Wiley, 2010 Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein, Introduction to Algorithms, 3 rd Ed. Cambridge, MA, MIT Press, 2009 14

CLASS RULES � Be Respectful and Responsible No talking among yourselves � No cell

CLASS RULES � Be Respectful and Responsible No talking among yourselves � No cell phones/communication devices � � � Be Organized and Follow Directions Be Prepared � � If you miss a lecture, it is your responsibility to makeup Assignment Must Be Your Own Work It will help you understand the material � Cite all references � We may be using plagiarism detection tools � 15

EXPECTATIONS � All the students are required to read the assigned sections of the

EXPECTATIONS � All the students are required to read the assigned sections of the book Please keep up with the reading � In-class activities and homework will assume that you have done this. � � � All the students are expected to actively participate in the in-class activities If you have difficulties in homework, bring them to class and ask during the breaks 16

QUESTIONS OR CONCERNS? 17

QUESTIONS OR CONCERNS? 17

DISCUSSION & SURVEY 18

DISCUSSION & SURVEY 18

INTRODUCTION TO ALGORITHMS 19

INTRODUCTION TO ALGORITHMS 19

LEARNING OUTCOMES � After successfully studying contents covered in this lecture, students should be

LEARNING OUTCOMES � After successfully studying contents covered in this lecture, students should be able to, explain the idea of a computational task � explain what an algorithm is and express an algorithm using pseudo code or flowcharts � explain the concept of an abstract data type � write pseudo codes for operations of linked list, stack, queue, and dictionary data structures � implement linked list, stack, queue, and dictionary data structures and use them � 20

INTRODUCTION � Why Do We Study Data Structures and Algorithms? To make understanding and

INTRODUCTION � Why Do We Study Data Structures and Algorithms? To make understanding and solving problems simple � To solve problems in the BEST way � 21

SAMPLE PROBLEMS � � � Searching the Web for the IESL College of Engineering

SAMPLE PROBLEMS � � � Searching the Web for the IESL College of Engineering Calculating the Fourier Transform of a Signal Designing a PCB Layout for a Circuit Sorting a List of Names Calculating Best Path from Colombo to Anuradhapura 22

SAMPLE PROBLEMS � � � Searching the Web for the IESL College of Engineering

SAMPLE PROBLEMS � � � Searching the Web for the IESL College of Engineering Calculating Best Path from Colombo to Anuradhapura Calculating the Fourier Transform of a Signal Designing a PCB Layout for a Circuit Sorting a List of Names 23

COMPUTATIONAL TASK � A computational task is not just a “single” task such as

COMPUTATIONAL TASK � A computational task is not just a “single” task such as “Is 3962431 prime ? ” � “What is 37487*2371 ? ” � � A computational task is a whole family of “similar” tasks with varying INPUT, such as “Given a whole number A, is A prime? ” � “Given 2 numbers x and y, what is x times y? ” � 24

ALGORITHM � What is an Algorithm? Well defined procedure � Takes some inputs �

ALGORITHM � What is an Algorithm? Well defined procedure � Takes some inputs � Produce some outputs � � An algorithm is a step-by-step method of solving a computational task 25

SPECIFYING ALGORITHMS � � Listing the Steps Flowcharts Pseudocode Program Listing 26

SPECIFYING ALGORITHMS � � Listing the Steps Flowcharts Pseudocode Program Listing 26

FLOWCHARTS � � A diagram that show the “flow of control” of an algorithm/a

FLOWCHARTS � � A diagram that show the “flow of control” of an algorithm/a program Flowchart symbols 27

FLOWCHART SYMBOLS � Terminals � � Represents the stat or end of the process

FLOWCHART SYMBOLS � Terminals � � Represents the stat or end of the process Represented by rounded rectangles Start � End Input/output Data � � Represented by parallelograms Indicate an input or output operation Read input x Output Max Value 28

FLOWCHART SYMBOLS � Stored Data � � Represented by rectangles Indicates a process such

FLOWCHART SYMBOLS � Stored Data � � Represented by rectangles Indicates a process such as a mathematical computation or variable assignment Stored data value � Processing � � Represented by rectangles Indicates a process such as a mathematical computation or variable assignment x=x+1 y= y+5 Increment x add 5 to y 29

FLOWCHART SYMBOLS � Decision � � Yes Represented by diamond shape Indicates different paths

FLOWCHART SYMBOLS � Decision � � Yes Represented by diamond shape Indicates different paths of execution/choices the program can take Is end of the array? No Yes More employees ? No 30

FLOWCHARTS � Example � � Draw the flowchart of an algorithm to add all

FLOWCHARTS � Example � � Draw the flowchart of an algorithm to add all the numbers in a list of numbers Class Activity � Draw the flowchart of an algorithm to find and print the largest number in a list of numbers 31

PSEUDOCODE � � � Human readable version of program code An intermediate level between

PSEUDOCODE � � � Human readable version of program code An intermediate level between program code and listing of steps Each step should be simple and precise � � � Can use predefined procedures Example: Express an algorithm to add all the numbers in a list of numbers using pseudocode Class Activity: Express an algorithm to find and print the largest number in a list of numbers using pseudocode 32

BASIC DATA STRUCTURES 33

BASIC DATA STRUCTURES 33

INTRODUCTION � What is a data structure? A way to store and organize data

INTRODUCTION � What is a data structure? A way to store and organize data � Operations performed on them � � � Facilitates creation, access and modification Examples � Records, arrays, link lists, stacks, queues, trees, graphs 34

INTRODUCTION � What is a ADT? Data structure specification � A set of operations

INTRODUCTION � What is a ADT? Data structure specification � A set of operations on the structure � Implementation independent � � Importance of ADT � Data abstraction � � Allows us to focus on important aspects of data Encapsulation or information hiding 35

INTRODUCTION � ADT specification � The set of operations on the structure Name �

INTRODUCTION � ADT specification � The set of operations on the structure Name � Input/Pre condition � Process � Output/Post condition � � Types of operations Constructors � Access procedures � Manipulation procedures � 36

ARRAYS � � A finite set of homogeneous elements Stored in continuous memory locations

ARRAYS � � A finite set of homogeneous elements Stored in continuous memory locations Calculating address of elements 2 D arrays � Row major vs column major ordering 37

ARRAYS � Types Multidimensional arrays � Arrays that can grow � Dynamic arrays �

ARRAYS � Types Multidimensional arrays � Arrays that can grow � Dynamic arrays � � Strings as character arrays In c: null terminated character arrays � In java: string has a length � � Which type of array to use will depend on Requirements � Programming language construct � � Arrays, Array. List, and Vector 38

RECORDS � A simple data structure Stores heterogeneous data � Also called tuple or

RECORDS � A simple data structure Stores heterogeneous data � Also called tuple or struct � � Example � A record to store student name, index no, and current. GPA Index no: integer � name: string � Current. GPA: floating point � 39

ADT - LINKED LIST � � A set of linked elements Consists of Nodes

ADT - LINKED LIST � � A set of linked elements Consists of Nodes (Elements) � An attribute called head which specifies the first node of the linked list � � Node composition Data fields � Pointers � 40

LINKED LIST - SPECIFICATION � LIST-SEARCH(L, k) Finds the first node with the key

LINKED LIST - SPECIFICATION � LIST-SEARCH(L, k) Finds the first node with the key k in the list L � Returns NIL if no element is found � � LIST-INSERT(L, x) � � Inserts the node x into the front of the list L LIST-DELETE(L, x) � Deletes the node x from the list L if exists 41

LINKED LIST - TYPES � Linked list � Doubly linked list � Circular linked

LINKED LIST - TYPES � Linked list � Doubly linked list � Circular linked list 42

LINKED LIST - SPECIFICATION � Extensions � LIST-INSERT(L, x, i) � � Inserts the

LINKED LIST - SPECIFICATION � Extensions � LIST-INSERT(L, x, i) � � Inserts the node x into the ith location of the list L Modifications for double linked lists � LIST-INSERT-BEFORE (L, x, i) � � Inserts the node x before the ith element of the list L LIST-INSERT-AFTER(L, x, i) � Inserts the node x after the ith element of the list L 43

LINKED LIST - IMPLEMENTATION � C Using pointers � Allocate & free memory dynamically

LINKED LIST - IMPLEMENTATION � C Using pointers � Allocate & free memory dynamically � � C++/Java/Python Classes � Create objects and link them � � Comparison between lists in Python and the linked lists 44

ADT - QUEUE � � � A first in first out (FIFO) data structure

ADT - QUEUE � � � A first in first out (FIFO) data structure Similar to the queue you see in day to day life Insert -> enqueue � � Delete -> dequeue � � Insert element to the end of the queue Remove the first element from the queue Priority queues 45

ADT - STACK � � A last in first out (LIFO) data structure Insert

ADT - STACK � � A last in first out (LIFO) data structure Insert -> push � � Insert element to the top of the stack Delete -> pop � Remove the element from top of the stack 46

ADT - DICTIONARY � � Represents a set Operations Search (S, k) � Insert

ADT - DICTIONARY � � Represents a set Operations Search (S, k) � Insert (S, x) – add element to the dictionary � Delete (S, x) � Minimum (S) � Maximum (S) � Successor (S, x) � Predecessor (S, x) � 47

SELF STUDYING 48

SELF STUDYING 48

HOMEWORK � � � Write pseudo codes for the operations of a (single) linked

HOMEWORK � � � Write pseudo codes for the operations of a (single) linked list Write pseudo codes for the operations of a double linked list How can you implement a stack and a queue using a linked list? Write the pseudo codes for the operations � Will there be any advantages if you use a double linked list? � 49

HOMEWORK � How can you implement a dictionary using a linked list? What type

HOMEWORK � How can you implement a dictionary using a linked list? What type of a linked list (single linked list, double linked list or a circular linked list) would be the best option? Why? � What will be the answer to the above question if we remove the “Predecessor ” operation? � Write the pseudo codes for the operations � 50

REFERENCES [1] T. H. Cormen, C. E. Leiserson, R. L. Rivest and C. Stein,

REFERENCES [1] T. H. Cormen, C. E. Leiserson, R. L. Rivest and C. Stein, Introduction to Algorithms, 3 rd Ed. Cambridge, MA, MIT Press, 2009. [2] S. Baase and Allen Van Gelder, Computer Algorithms: Introduction to Design and Analysis, 3 rd Ed. Delhi, India, Pearson Education, 2000. 51