Академический Документы
Профессиональный Документы
Культура Документы
Fahad Maqbool
1
Problems
thought of as inputs and matching outputs. definition should include constraints on the resources that may be consumed by any acceptable solution.
Problem
Resource (Space)
OUTPUT
Resource (Time)
4
Examples - cont.
Searching an online phone directory: Linear search? OK for Calvin College too slow for Grand Rapids or New York
Amount of data is an important factor.
Restructure (order) the data set for efficient processing use binary search or an indexed sequential search
Compiler lookup of an identifier's type, etc. in a symbol table: Linear search? No, too slow Binary search? No, too much work to keep sorted Number of accesses & speed required is an important factor. Use hash tables
Text processing: Store in an array / vector? OK for text analysis word counts, average word length, etc. Not for word-processing Too inefficient if many insertions & deletions
Static vs. dynamic nature of the data is an important factor
6
Design Issue:
select
and design appropriate data types. (This is the real essence of OOP.)
7
Organizing Data
The choice of data structure and algorithm can make the difference between a program running in a few seconds or many days. The selection of the correct data structure is the difference between success and failure. Any organization and collection of records should be searchable, processable, modifiable in any order.
8
Are all data inserted into the data structure at the beginning, or are insertions interspersed with other operations?
e.g. Seats for TFA Basic operations: find empty seat, reserve a seat, cancel a seat assignment Why "abstract?" Data, operations, and relations are studied independent of implementation.
What not how is the focus.
10
Dynamic sets
Sets are as fundamental to Computer Sciences as they are to Mathematics. Mathematical sets are fixed, but sets in Computer science can shrink and grow or change over time. We call them dynamic sets. In the domain of algorithms, we need to perform set operations that are different from those of mathematics.
11
Dynamic sets
If we only have to implement INSERT, DELETE and test MEMBERSHIP on a dynamic set, we call such a set as a Dictionary. For a dynamic set we assume that one of the fields is an identifying fields, called as the key field.
12
Dynamic sets
(Operations)
Search (S, k) Insert (S, x) Delete (S, x) Minimum/Maximum (S)
Successor (S, x)
Predecessor (S, x)
13
last
Last Element
empty
maxlength
14
Linked Lists
Flexible
space use Dynamically allocate space for each element as needed Include a pointer to the next item Linked list Data Each node of the list contains
Next
the data item (an object pointer in our ADT) object a pointer to the next node Collection structure has a pointer HEAD
15
Linked Lists
Add time
Constant
Search time
Worst
case O(n)
object2
object
16
Comparison of methods
Array implementation requires us to specify max size at compile time If can not put a bound on the size of list, may use pointers Certain operations take longer in one implementation than the other. In case of values changing at certain locations, pointers can NOT be used. Array implementation may waste space. Pointer implementation uses only as much space, as needed on the list, BUT requires pointer space.
17
18
Take adjacency matrix as input from the user using a text file, where first two rows will represent #rows and #columns respectively.
Implement the graph using array data structure and linked list. Display the graph with vertices on the parameter of the circle. Perform the following operations on the graph while calculating the time and space requirements
20
Input format
4 5 1 0 1 0
0 0 1 1 1 0 1 0 1 0 1 1 1 0 1 0
21