Anda di halaman 1dari 6

DATA STRUCTURES NOTES

In computer science, a data structure is a particular way of storing and organizing data in a computer so that it can be usedefficiently. Different kinds of data structures are suited to different kinds of applications, and some are highly specialized to specific tasks. For example, B-trees are particularly well-suited for implementation of databases, while compiler implementations usually use hash tables to look up identifiers. Data structures provide a means to manage large amounts of data efficiently, such as large databases and internet indexing services. Usually, efficient data structures are a key to designing efficient algorithms. Some formal design methods and programming languages emphasize data structures, rather than algorithms, as the key organizing factor in software design. Storing and retrieving can be carried out on data stored in both main memory and in secondary memory.

Overview

An array stores a number of elements in a specific order. They are accessed using an integer to specify which element is required (although the elements may be of almost any type). Arrays may be fixed-length or expandable. Records (also called tuples or structs) are among the simplest data structures. A record is a value that contains other values, typically in fixed number and sequence and typically indexed by names. The elements of records are usually called fields or members. A hash table (also called a dictionary or map) is a more flexible variation on a record, in which name-value pairs can be added and deleted freely. A union type specifies which of a number of permitted primitive types may be stored in its instances, e.g. "float or long integer". Contrast with a record, which could be defined to contain a float and an integer; whereas, in a union, there is only one value at a time. A tagged union (also called a variant, variant record, discriminated union, or disjoint union) contains an additional field indicating its current type, for enhanced type safety. A set is an abstract data structure that can store specific values, without any particular order, and with no repeated values. Values themselves are not retrieved from sets, rather one tests a value for membership to obtain a boolean "in" or "not in". Graphs and trees are linked abstract data structures composed of nodes. Each node contains a value and also one or more pointers to other nodes. Graphs can be used to represent networks, while trees are generally used for sorting and searching, having their nodes arranged in some relative order based on their values.

An object contains data fields, like a record, and also contains program code fragments for accessing or modifying those fields. Data structures not containing code, like those above, are called plain old data structures.

Many others are possible, but they tend to be further variations and compounds of the above.

Basic principles
Data structures are generally based on the ability of a computer to fetch and store data at any place in its memory, specified by an addressa bit string that can be itself stored in memory and manipulated by the program. Thus the record and array data structures are based on computing the addresses of data items with arithmetic operations; while the linked data structures are based on storing addresses of data items within the structure itself. Many data structures use both principles, sometimes combined in non-trivial ways (as in XOR linking). The implementation of a data structure usually requires writing a set of procedures that create and manipulate instances of that structure. The efficiency of a data structure cannot be analyzed separately from those operations. This observation motivates the theoretical concept of an abstract data type, a data structure that is defined indirectly by the operations that may be performed on it, and the mathematical properties of those operations (including their space and time cost).

Language support
Most assembly languages and some low-level languages, such as BCPL (Basic Combined Programming Language), lack support for data structures. Many high-level programming languages and some higher-level assembly languages, such as MASM, on the other hand, have special syntax or other built-in support for certain data structures, such as vectors (onedimensional arrays) in theC language or multi-dimensional arrays in Pascal. Most programming languages feature some sort of library mechanism that allows data structure implementations to be reused by different programs. Modern languages usually come with standard libraries that implement the most common data structures. Examples are the C++ Standard Template Library, the Java Collections Framework, and Microsofts .NET Framework. Modern languages also generally support modular programming, the separation between the interface of a library module and its implementation. Some provide opaque data types that allow clients to hide implementation details. Object-oriented programming languages, such as C++, Java and Smalltalk may use classes for this purpose. Many known data structures have concurrent versions that allow multiple computing threads to access the data structure simultaneously.

Graph operations and representation: Path problems: Since a graph may have more than one path between two vertices, we may be interested in finding a path with a particular property. For example, find a path with the minimum length from the root to a given vertex (node) Definitions and Connectedness problems: o Simple path: a path in which all vertices, except possibly the first and last, are different o Undirected graph: a graph whose vertices do not specify a specific direction o Directed graph: a graph whose vertices do specify a specific direction o Connected graph: there is at least one path between every pair of vertices o Bipartite graphs: graphs that have vertexes that are partitioned into 2 subsets A and B, where every edge has one endpoint in subset A and the other endpoint in subset B o A complete graph: an n-vertex undirected graph with n(n-1)/2 edges is a complete graph o A complete digraph: (denoted as Kn) for n-vertices a complete digraph contains exactly n(n-1) directed edges o Incident: the edge (i, j) is incident on the vertices i and j (there is a path between i and j) o In-degree: the in-degree d of vertex i is the # of edges incident to i (the # of edges coming into this vertex) o The out-degree: the out-degree d of vertex i is the # of edges incident from vertex i (the # of edges leaving vertex i) o The degree of a vertex: the degree d of vertex i of an undirected graph is the number of edges incident on vertex i o Connected component: a maximal sub-graph that is connected, but you cannot add vertices and edges from the original graph and retain connectedness. A connected graph has EXACTLY one component o Communication network: Each edge is a feasible link that can be constructed. Find the components and create a small number of feasible links so that the resulting network is connected o Cycles: the removal of an edge that is on a cycle does not affect the networks connectedness (a cycle creates a sort of loop between certain vertices, for example there is a path that links vertex a to b to c and then back to a) Spanning problems: o A spanning tree: is a sub-graph that includes all vertices of the original graph without cycles Start a breadth-first search at any vertex of the graph If the graph is connected, the n-1 edges are used to get to the unvisited vertices define the spanning tree (breadth-first spanning tree) Graph representation: a graph is a collection of vertices (or nodes), pairs of which are joined by edges (or lines) Diagonal entries are zero for both directed and non-directed graphs The adjacency matrix of an undirected graph is symmetric The adjacency matrix of a directed graph need not be symmetric Needs n2 bits of space, for an undirected graph we only need to store the upper or lower triangle (since they are symmetric) Time to find the vertex degree and/or vertices adjacent to is O(n)

Adjacency lists: an adjacency list for vertex i is a linear list of vertices adjacent from vertex i. These are much more time efficient then an adjacency matrix. Graph search methods: a vertex u is reachable from vertex b iff there is a path from u to b o A search method starts at a given vertex v and visits/labels/marks every vertex that is reachable from v o Many graph problems are solved using search methods Breadth-first search: (Pseudocode and the actual code are found on page 8) _ Visit the start vertex and use a FIFO queue _ Repeatedly remove a vertex from the queue, visit its unvisited adjacent vertices putting the newly visited vertices into the queue, when the queue is empty the search terminates _ All vertices that are reachable from the start vertex (including the start vertex) are visited Depth-first search: _ Has the same complexity as breadth-first search _ Has the same properties with respect to path finding, connected components, and spanning trees _ Edges used to reach unvisited vertices define a depth-first spanning tree when the graph is connected _ There are problems were each method performs better than the other

We start at vertex 1, label it as visited Do a DFS on the unvisited neighbors of 1 = nodes 2, 3, 9 Visit 2, label it as visited, and do a DFS on its unvisited neighbors = 4, 5, 8 Visit 4, label it as visited, and do a DFS on its unvisited neighbors = 8 Visit 8, label it as visited, and do a DFS on its unvisited neighbors = nobody Since 8 has no unvisited neighbors we return to its parent = 4 and visit any unvisited neighbors = nobody

Since 4 has no unvisited neighbors we return to its parent = 2 and visit any unvisited neighbors =5 Visit 5, label it as visited, and do a DFS on its unvisited neighbors = 9 Visit 9, label it as visited, and do a DFS on its unvisited neighbors = nobody Since 9 has no unvisited neighbors we return to its parent = 5 and visit any unvisited neighbors = nobody Since 5 has no unvisited neighbors we return to its parent = 2 and visit any unvisited neighbors = nobody Since 2 has no unvisited neighbors we return to its parent = 1 and visit any unvisited neighbors =3 Visit 3, label it as visited, and do a DFS on its unvisited neighbors = 6, 7 Visit 6, label it as visited, and do a DFS on its unvisited neighbors = nobody Since 6 has no unvisited neighbors we return to its parent = 3 and visit any unvisited neighbors =7 Visit 7, label it as visited, and do a DFS on its unvisited neighbors = nobody We are done, the print out would appear as follows: 1, 2, 4, 8, 5, 9, 3, 6, 7 A Breadth-first-search would yield the following print out: 1, 2, 3, 9, 4, 5, 8, 6, 7 Quick sort: _ Small instances are when n<=1 where n is an element (remember that every instance this small is considered already sorted) ._ To sort the large instances select a pivot element from the n elements ._ Partition the n elements into 3 groups (left, middle, and right) where the middle group contains only the pivot elements, all the left groups elements are smaller than or equal to the middle groups elements, and the right groups elements are larger than or equal to the middle groups elements. The middle group only contains one element, the pivot point. ._ Use recursion to sort the left and right groups ._ The answer is the sorted left group, followed by the middle group, followed by the right group ._ REMEMBER the pivot is the leftmost element in the list to be sorted, so it would be position zero in an array, or we could randomly select a pivot point, or the median-of-three rule. If we randomly choose our pivot point or we use the median-ofthree rule we do not need to change our quick sort code, we simply swap the positions of the pivot point and position zero in the array. ._ The median-ofthree rule: for this rule the pivot is chosen to be the median of the three elements {a[leftEnd], a[(leftEnd+rightEnd)/2], a[rightEnd]}. For Example if these elements have keys {5, 9, 7}, then a[rightEnd] is used as the value of pivot because 5 <= 7 <=9 (in other words ask yourself what is the key in a[0], a[a.length -1], and a[(a.length-1)/2]. Which ever key is the middle value is chosen as the pivot point). Once the pivot has been chosen simply swap its position with the first position in the array and then proceed from this point as if we were using the leftmost element rule.

MCQ QUESTIONS FOR DATA STRUCTURE.


Click Below Link To DOWNLOAD PDF FILE
http://www.ebookspdf.org/view/aHR0cDovL3d3dy5pbmRpYXN0dWR5Y2hhbm5lbC5jb20vYXR0YWNobWVudHMvUm Vzb3VyY2VzLzEzMjg4OS0xMjkzMi1kYXRhLXN0cnVjdHVyZXMtYS1hbGdvcml0aG1zLW11bC5wZGY=/RGF0 YSBTdHJ1Y3R1cmVzICYgQWxnb3JpdGhtcyAtIE11bHRpcGxlIENob2ljZSBRdWVzdGlvbnMgKG1jcXM=

Click Below Link To DOWNLOAD PDF FILE


http://www.ebookspdf.org/view/aHR0cDovL2lldGUtZWxhbi5hYy5pbi9Tb2xRUC9zb2xuL0RDMDhfc29sLnBkZg==/VHlw aWNhbCBRdWVzdGlvbnMgJiBBbnN3ZXJz

Click Below Link To DOWNLOAD PDF FILE


http://www.ebookspdf.org/view/aHR0cDovL2ZhY3VsdHkubXUuZWR1LnNhL2Rvd25sb2FkLnBocD9maWQ9Nzc1Nzc=/ U3R1ZHkgUXVlc3Rpb25z

Anda mungkin juga menyukai