



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Data abstraction, its importance, and common data structures such as queues, stacks, sets, relations, and maps. Examples of their usage and implementation in perl. Data abstraction is the process of hiding implementation details and providing a simplified interface for interacting with complex systems.
Typology: Lecture notes
1 / 27
This page cannot be seen from the preview
Don't miss anything!




















#15: let the ordered pairs (x,y) be over the integers. Define a relationship between (a,b) and (c,d) whenever a+d = b+c. Show this is an equivalence relation. Let (a,b) and (c,d) be chosen so that a+d = b+c. Then a-b = c-d, which means that two pairs are related iff their difference is the same. An equivalence relation is symmetric, transitive, and reflexive. Reflexive: compare (a,b) with (a,b); a-b=a-b, so ((a,b),(a,b)) is in the relation and the relation is reflexive. Symmetric: Suppose that ((a,b),(c,d)) is in the relation. then a-b = c-d, so c-d = a-b, because = is symmetric, so ((c,d),(a,b)) is in the relation. Transitive: Suppose that ((a,b),(c,d)) is in the relation, and ((c,d),(e,f)) is in the relation. Then a-b = c-d and c-d = e-f, so a-b = e-f, so ((a,b),(e,f)) is in the relation, so the relation is transitive. Thus the relation is reflexive, symmetric, and transitive, Homework Monday, November 29, 2010 3:03 PM Docsity.com
Thus the relation is reflexive, symmetric, and transitive, so it is an equivalence relation. Docsity.com
Data abstraction is the process of encapsulating data manipulation and naming it in high-level terms. We could, in principle, do everything without data abstraction. But our code would be much harder to read. Example: breadth-first search for path distance. Doing without data abstraction Monday, November 29, 2010 2:09 PM Docsity.com
From last time: Finding the distance between two vertices v and w Start by labeling v with distance 0 Enqueue v Dequeue x set dist(n) to dist(x)+ Enqueue n if there is no distance known to n, end if for each neighbor n of x end for while queue not empty, end while At the end, distances are known from v to everything. Finding the distance between two vertices Sunday, November 21, 2010 6:23 PM Docsity.com
(i.e., a set of pairs) a relation that describes the edges. (i.e., a partial function from vertices to numbers) a map that stores the computed distances (i.e., a first-in, first-out device) a queue that remembers the order in which to visit vertices. To do breadth first search, you need: Enter data abstraction Monday, November 29, 2010 2:26 PM Docsity.com
"Remember" how a structure is used. "Forget" how a structure is built. "Keep less details" in your head. The game of data abstraction The high-level code "looks like" the pseudo-code that describes the overall algorithm. The overall goal of data abstraction: Data abstraction Sunday, November 28, 2010 5:14 PM Docsity.com
Interface: how one uses something. Implementation: how it's built. Interface and implementation: $q = new Queue; $q->enqueue("Fred"); $q->enqueue("George"); $thing=$q->dequeue;
while (!$q->empty) { } print "q is ". $q->toString. "\n"; Queue usage: Queue implementation: see http://www.cs.tufts.edu/comp/14/examples/Data/Queue.pm Interface and implementation Sunday, November 28, 2010 5:20 PM Docsity.com
The main advantage of data abstraction: abstract code looks like pseudo-code pseudo-code without abstraction with abstraction while queue not empty while (@q!=0) { … } while (!$q->empty) { ... } enqueue v push(@q,$v); $q->enqueue($v); dequeue x $x = shift(@q); $x = $q->dequeue; you need to remember that enqueue is push and dequeue is shift! Without abstraction, you don't have to remember. With abstraction, Advantages of data abstraction Monday, November 29, 2010 2:19 PM Docsity.com
Can vary the implementation Without varying the interface And all works as before! Why is data abstraction important? It is extremely important to act only through the interface, so that If someone changes the implementation, your code will still work. But Why is data abstraction important? Monday, November 29, 2010 7:05 AM Docsity.com
http://www.cs.tufts.edu/comp/14/examples/Data/Queue2.pm Suppose we write a somewhat different queue: The only difference is that internally, the queue is stored backward! Queue.pm and Queue2.pm are functionally identical. You "don't need to know" that the internal order is reversed. http://www.cs.tufts.edu/comp/14/examples/Data/Queue.perl http://www.cs.tufts.edu/comp/14/examples/Data/Queue2.perl Demonstration: Do exactly the same thing! Suppose we write a different queue: Sunday, November 28, 2010 5:33 PM Docsity.com
$s = new Stack; # make one $s->push($thing); # put an element in $thing = $s->pop; # take an element out $thing = $s->top; # top element, without popping $empty = $s->empty; # check whether empty A stack has the interface: http://www.cs.tufts.edu/comp/14/examples/Data/Stack.pm See http://www.cs.tufts.edu/comp/14/examples/Data/Stack.perl and to use it, see Stack Monday, November 29, 2010 9:46 AM Docsity.com
$s = new Set; $s->add($member); $s->remove($member); … if ($s->contains($thing)) { } $u = $s->union($t); $d = $s->difference($t); $i = $s->intersection($t); @contents = $s->contents; A Set has the interface And its implementation is: http://www.cs.tufts.edu/comp/14/examples/Data/Set.pm And a demonstration of its use: http://www.cs.tufts.edu/comp/14/examples/Data/Set.perl Set Monday, November 29, 2010 11:33 AM Docsity.com
my $m = new Map; $m->set('foo', 'bar'); $v = $m->get('foo'); $m->remove('foo'); @keys = $m->keys; A Map is a partial function. Examples of use: Its implementation: http://www.cs.tufts.edu/comp/14/examples/Data/Map.pm and an example of use http://www.cs.tufts.edu/comp/14/examples/Data/Map.perl Maps Monday, November 29, 2010 1:07 PM Docsity.com
A graph G = (V,E) where V is a set and E is a relation. So, what is a graph? $g->{'vertices'} = new Set; $g->{'edges'} = new Relation; So we can define a graph in terms of those data types! $g = new MyGraph; $g->addEdges('Frank','Sue'); $g->removeEdges('Frank','Sue'); $g->addVertices('George'); $g->removeVertices('George'); @neighbors = $g->successors('Frank'); @edges = $g->edges->contents; @vertices = $g->vertices->contents; Part of interface for MyGraph is: The implementation of MyGraph is: http://www.cs.tufts.edu/comp/14/examples/Data/MyGraph.pm and an example of use: http://www.cs.tufts.edu/comp/14/examples/Data/MyGraph.perl So, what is a graph? Monday, November 29, 2010 11:43 AM Docsity.com