4Why study data structures? Cpt S 223Why study data structures?Example problemGiven: a set of N numbersGoal: search for number kSolutionStore numbers in an array of size NLinearly scan array until k is found or array is exhaustedNumber of checksBest case: 1Worst case: NAverage case: N/2376110843Washington State University
5Why study data structures? Cpt S 223Why study data structures?Solution #2Store numbers in a binary search treeSearch tree until find kNumber of checksBest case: 1Worst case: log2NAverage case: (log2N)/2731016843Washington State University
6Analysis Does it matter? N vs. log2N Cpt S 223 Washington State University
7Analysis Does it matter? Assume 1 cycle per transaction O(N) algorithm Cpt S 223AnalysisDoes it matter?AssumeN = 1,000,000,0001 billion (Walmart transactions in 100 days)1 GHz processor = 109 cycles per second1 cycle per transactionO(N) algorithm1 billion transactions = > 1 billion clock cyclesO(lg N) algorithm1 billion transactions => 30 clock cyclesWashington State University
8Example 2 Scheduling job in a printer Cpt S 223Example 2Scheduling job in a printerWrite a code to manage the printer queueFunctions to supportInsert, deleteSpecial accommodations needed for:PriorityDynamic updateScheduling challengesWashington State University
9Example 3 Exploring the Facebook connection network Cpt S 223Example 3Exploring the Facebook connection networkWrite a code to tell who is connected to who (directly or indirectly) through your Facebook profile6-degrees of separationWashington State University
10Example 4 Pattern matching Cpt S 223Example 4Pattern matchingWrite a code to do Google search on your web databaseWashington State University
11Summary Keep the data organized Choice of data structures matters Cpt S 223SummaryKeep the data organizedChoice of data structures mattersAppropriate data structures ease design & improve performanceChallengeDesign appropriate data structure & associated algorithms for a problemAnalyze to show improved performanceWashington State University
12Queue ADTLike a stack, a queue is also a list. However, with a queue, insertion is done at one end, while deletion is performed at the other end.Accessing the elements of queues follows a First In, First Out (FIFO) order.Like customers standing in a check-out line in a store, the first customer in is the first customer served.
13: Basic operations enqueue: insert an element at the rear of the list dequeue: delete the element at the front of the list
14Implementation of Queue Just as stacks can be implemented as arrays or linked lists, so with queues.Dynamic queues have the same advantages over static queues as dynamic stacks have over static stacks
15Trees Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search, insert, delete) is O(log N)?TreesBasic conceptsTree traversalBinary treeBinary search tree and its operations
16Trees A tree T is a collection of nodes T can be empty (recursive definition) If not empty, a tree T consists ofa (distinguished) node r (the root),and zero or more nonempty subtrees T1, T2, ...., Tk
18Tree Traversal Used to print out the data in a tree in a certain order Pre-order traversal (Root, left, right)Print the data at the rootRecursively print out all data in the leftmost sub treeRecursively print out all data in the rightmost sub tree
24Binary TreesA tree in which no node can have more than two children
25Continue..The depth of an “average” binary tree is considerably smaller than N, even though in the worst case, the depth can be as large as N – 1.
26Binary Search Trees (BST) A data structure for efficient searching, inser-tion and deletionBinary search tree propertyFor every node XAll the keys in its left subtree are smaller than the key value in XAll the keys in its right subtree are larger than the key value in X
28GraphA Simple graph G = (V, E) consists of a nonempty set V of vertices and a possibly empty set E of edges, each edge being a set of two vertices from V.The number of vertices and edges are denoted by |V| and |E|, respectively.
29Graph Representation There are variety of ways to represent a graph. Adjacency lists.An adjacency matrix of graph G = (V, E) is a binary |V| x |V| matrix such that each entry of this matrixAn incident matrix of graph G = (V, E) is a binary |V| x |E| matrix such that each entry of this matrix
32Graph TraversalAs in trees, traversing a graph consists of visiting each vertex only one time. The simple traversal algorithm used for trees can not be applied here because graph may include cycles (result in to infinite loop) or, isolated vertices (left some nodes).The most popular algorithms for traversing in graphs are:Depth First SearchBreadth First search