180 likes | 636 Views
Introduction to Data Structures and Algorithms. Chien -Chung Shen CIS/UD cshen@udel.edu. Data Structures. A data structure is a way of organizing, storing, and performing operations on data Examples: array, list, stack, tree, heap, graph
E N D
Introduction to Data Structures and Algorithms Chien-Chung Shen CIS/UD cshen@udel.edu
Data Structures • A data structure is a way of organizing, storing, and performing operations on data • Examples: array, list, stack, tree, heap, graph • The choice of data structures used in a program (application) depends on both the type of data being stored and the operations the program may need to perform on that data • e.g., array vs. (linked) list • there are no bad (or good) data structures, but only “suitable” data structures
Algorithms • An algorithm describes a sequence of steps to solve a computational problem or perform a calculation • An algorithm can be described in English, pseudocode, a programming language, hardware (e.g., ASIC), etc. • A computational problem specifies an input, a question about the input that can be answered using a computer, and the desired output • Jeannette M. Wing, Computational Thinking, Communications of the ACM, Volume 49, Issue 3, pp. 33-35, March 2006 • A skill in addition to reading, writing, and arithmetic • Video lecture 1 and video lecture 2
Efficient Algorithms and Hard Problems • Algorithm efficiency is most commonly measured by the algorithm runtime, and an efficient algorithm is one whose runtime increases no more than polynomiallywith respect to the input size • There are problems where the existence of “efficient algorithms” are unknown • NP-complete problems are a set of problems for which no known efficient algorithm exists • Characteristics of NP-complete problems • no efficient algorithm has been found to solve an NP-complete problem • no one has proven that an efficient algorithm to solve an NP-complete problem is impossible • if an efficient algorithm exists for one NP-complete problem, then all NP-complete problem can be solved efficiently
Efficient Algorithms and Hard Problems • By knowing a problem is NP-complete, instead of trying to find an efficient algorithm to solve the problem optimally, people focus on finding an algorithm to efficiently find a good, but non-optimal, solution
Clique Problem • The clique problem is the computational problem of finding cliques (subsets of vertices, all adjacent to each other (termed complete subgraphs) in a graph 2-clique in 7-vertex graph 3-clique in 7-vertex graph 4-clique in 7-vertex graph
4-clique “brute force” algorithm C(7, 4) = ?
Relation between Data Structures and Algorithms • Program = Data Structures + Algorithms • Data structures not only define how data is organized and stored, but also the operations performed on the data structure • data structure chosen → algorithms developed • e.g. array vs. list • Some algorithms utilize data structures to store and organize data during the algorithm execution • OS uses linked lists, tables (arrays), etc.
Abstract Data Types • In computer science (esp. languages), the notion of type is very crucial • e.g., “integer” type • what does the type of a variable tell you? • the (legal) operations you could work on it • https://en.wikipedia.org/wiki/Type_system • An abstract data type (ADT) is a data type described by predefined user operationswithoutindicating how each operation is implemented
Abstract Data Types • Can you think about that a “car” is an ADT? • What operations can you “operate” on a car? • accelerate, break, turn, etc. • Do you know how these operations work? • probably not • gas engine vs. electric Car tesla; tesla.break(); tesla.accelerate();
Is Keurig an ADT? Keurig k; k.open(); k.brew();
The Power of Abstraction • Barbara Liskov (ACM A.M. Turing Award lecture), 2007 • For contributions to practical and theoretical foundations of programming language and system design, especially related to data abstraction, fault tolerance, and distributed computing
Abstraction vs. Optimization • There is no free lunch – “tradeoff” of ADT • abstraction hides underlying implementation • Using ADTs enables programmers or algorithm designers to focus on higher-level operations and algorithms, thus improving programmerefficiency • However, knowledge of the underlying implementation is needed to analyze or improve the runtime efficiency • We will be learning both in this class!
ADTs in Standard Libraries #include <iostream> #include <vector> using namespace std; int main( ) { vector<int> squares( 100 ); for( inti = 0; i < squares.size( ); ++i ) squares[ i ] = i * i; for( inti = 0; i < squares.size( ); ++i ) cout << i << " " << squares[ i ] << endl; return 0; }
Algorithm Efficiency • Algorithm efficiency is typically measured by the algorithm's computational complexity • Computational complexity is the amount of resources used by the algorithm • The most common resources considered are the runtime (time) and memory usage (space) • Runtime complexity is a function T(N) representing the number of constant time operations performed by an algorithm on an input of size N • Space complexity is a function S(N) representing the number of fixed-size memory units used by an algorithm for an input of size N