Data Structures and Algorithms Lists, Stacks, Queues, and Graphs Sorting and searching algorithms.

Slides:



Advertisements
Similar presentations
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Advertisements

CS252: Systems Programming Ninghui Li Program Interview Questions.
Data Structures and Algorithms (60-254)
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
Abstract Data Types and Subprograms
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Abstract Data Types and Subprograms
Advanced Data Structures
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Using Search in Problem Solving
COMP 110 Introduction to Programming Mr. Joshua Stough.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Important Problem Types and Fundamental Data Structures
12 Abstract Data Types Foundations of Computer Science ã Cengage Learning.
Binary Trees Chapter 6.
1 Complexity Lecture Ref. Handout p
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Tree.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 5: Stacks and Queues.
ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Introduction to Data Structures. Definition Data structure is representation of the logical relationship existing between individual elements of data.
ECE 103 Engineering Programming Chapter 61 Abstract Data Types Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
Representing and Using Graphs
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
9-1 Abstract Data Types Abstract data type A data type whose properties (data and operations) are specified independently of any particular implementation.
Graphs. Made up of vertices and arcs Digraph (directed graph) –All arcs have arrows that give direction –You can only traverse the graph in the direction.
COMP261 Lecture 6 Dijkstra’s Algorithm. Connectedness Is this graph connected or not? A Z FF C M N B Y BB S P DDGG AA R F G J L EE CC Q O V D T H W E.
Stacks And Queues Chapter 18.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Heaps and basic data structures David Kauchak cs161 Summer 2009.
Chapter 8 Abstract Data Types and Subprograms. 2 Chapter Goals Distinguish between an array-based visualization and a linked visualization Distinguish.
COSC 2007 Data Structures II
Graphs Upon completion you will be able to:
Breadth-first and depth-first traversal Prof. Noah Snavely CS1114
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Breadth-first and depth-first traversal CS1114
Chapter 05 Introduction to Graph And Search Algorithms.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
DATA STRUCURES II CSC QUIZ 1. What is Data Structure ? 2. Mention the classifications of data structure giving example of each. 3. Briefly explain.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Extension of linked list
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
Chapter 12 Abstract Data Type.
ADT description Implementations
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Chapter 12 – Data Structures
Chapter 15 Lists Objectives
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Csc 2720 Instructor: Zhuojun Duan
Introduction to Data Structure
i206: Lecture 13: Recursion, continued Trees
Lesson Objectives Aims
Data Structures – Stacks and Queus
ITEC 2620M Introduction to Data Structures
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
CMSC 202 Trees.
Data Structures and Algorithms for Information Processing
CSC 380: Design and Analysis of Algorithms
Important Problem Types and Fundamental Data Structures
Lecture 9: Stack and Queue
Presentation transcript:

Data Structures and Algorithms Lists, Stacks, Queues, and Graphs Sorting and searching algorithms

Lists A list is a sequence of zero or more data items. The total number of items is said to be the length of the list. The length of a given list can grow and shrink on demand. The items can be accessed, inserted, or deleted at any position in a list.

Two ways to implement lists Array implementation Linked list implementation End of list array null

Stacks A stack is a special kind of list in which all insertions and deletions take place at one end, called the top. Therefore, it has another name ``pushdown list''. Its items are added and deleted on a last-in-first-out (LIFO) basis. In Java, “Stack s = new Stack()” creates a stack object.

Java Examples – using stacks Q. What is printed out by the following fragment of Java? Stack s = new Stack(); for(int i=0; i<10; i++) s.push(i); while(!s.empty()) System.out.printnl(s.pop);

Java examples – using stacks Q. What is printed out by the following fragment of Java? Stack s = new Stack(); for(int i=0; i<5; i++) s.push(i); while(!s.size() > 1) s.push(s.pop() + s.pop()); System.out.printnl(s.pop);

Queues A queue is another special kind of list, where items are inserted at one end (the rear) and deleted at the other end (the front). A queue is also called a FIFO, since the items are deleted in the same order as they were added - on a first-in-first-out basis. For a queue structure, we have two special names for insertion and deletion: ENQUEUE and DEQUEUE.

How to implement queues? Tricky! How to make both enqueue & dequeue operations efficient ? (avoid shifting items) enqueue tail= (tail+1) mod size dequeue head= (head+1) mod size Wrap around head tail

An example of using queue One printer is connected to several computers Printing a file takes much longer time than transmitting the data; a queue of printing jobs is needed When new job P arrives, do enqueue(P) When a job is finished, do dequeue(P)

Printing task queue ^ | | |_________ | jobs removed here (dequeue)new jobs added here (enqueue) job 1 job 2 job 3 job 4 job 5 job 6

Algorithms An algorithm is a step-by-step method of solving computational tasks. Algorithm = a precise sequence of actions for performing a computational task (independent from computer languages, i.e. pseudo code).

Computational Task ? A computational task is not just a “single” task such as “Is 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?”

Sorting- one of the most frequently used computational task Nature tends towards disorder Human prefer order e.g. address books shopping lists databases

Insertion sort Commonly used for hand-sorting Use two lists – unsorted and sorted unsorted = input list (assume that size = n) sorted = an empty list loop from i =0 to n-1 do { // n loops insert unsorted[i] to the sorted list // needs about n steps comparing or shifting } Can you write an ordered insertion algorithm?

Another sorting algorithm – quick sort (An example of using stack) Goal: sort numbers from position 1 to 10 into order (to simplify assume size=10) Sub-goal: sort numbers from position a to b into order. Easy case a = b. How to split goal into sub-goals? Use a partitioning method

partitioning Randomly pick a pivot. The rest of the numbers are shifted to either the left or right side of the pivot depending on whether it is greater or less than the pivot. Pivot is already in the sorted position picked pivot Pivot’s moved to here

Using a Stack push (1,10) onto stack while stack not empty do { pop (a,b) from stack partitioning (a,b) into (a,m-1) and (m+1,b) if(a < m-1) push (a,m) onto stack if(m+1 < b) push(m+1,b) onto stack }

Step by step

Memo for In-class test 9 [ /5] questionsmy answers correct answers comments

Back to data structures – something not a sequence … Graphs – examples of graphs Road maps (directed, cyclic, weighted) Project network (directed, cyclic, weighted) Electrical circuits Molecules Relationships (directed, un-weighted?) - family tree - students on courses

An abstract view of graphs A graph is a collection of nodes (vertices) which maybe connected in pairs by line segments called edges.

Trees – a special graph without cycles Hieratical graph Root – the only node at the topmost All rest of nodes must be linked to a parent note, and may have zero or more child nodes Leaf - Node without children root Child 1 leaf Child 2 leaf Child 3 leaf

A Searching Problem …… ‘My house is No. 42 and is 100 yards from the roundabout’, said your friend. Unfortunately you realise, as you reach the roundabout, that he hasn’t told you which exit to take. What should you do?

Even worse case, ‘ 100 yards from the 3rd roundabout ’, said your friend

Searching Algorithms - 1 Depth first with backtracking Go far as you can in one direction, stop if the problems solved. If reach ‘dead-end’, go back up to the last point where a decision was made, and make a different (untried) direction. If there are no new decision, back up a further choice point.

Searching Algorithms - 2 Breadth first Explore all possible directions in one step, and remember those intermediate stages Carry on exploring all intermediate stages, step by step, until a solution found.

An Example of Breadth First Search Finding a shortest path in a graph (say, from A to D) A E F D C B G H

Breadth first and Depth first Breadth first Uses a queue Visit all nearest nodes first Depth first Uses a stack Go as far as possible

Think about this You have to build a tower from a collection of small boxes of different sizes. The height of the tower must be 1m. How do you solve this problem?

Memo for In-class test 10 [ /5] questionsmy answers correct answers comments