3 Pages

lab1sol

Course: CSE 542, Spring 2012
School: Washington State
Rating:
 
 
 
 
 

Word Count: 1401

Document Preview

542 CSE Advanced Data Structures and Algorithms Lab 1 Solution Jon Turner Part 1. The source code for this part appears below. Dheap.h (with unchanged parts omitted) ... class Dheap { public: ... // stats methods void clearStats(); string& stats2string(string&) const; private: ... // statistics counters int changekeyCount; int siftupCount; int siftdownCount; ... }; Dheap.cpp ... void...

Register Now

Unformatted Document Excerpt

Coursehero >> Washington >> Washington State >> CSE 542

Course Hero has millions of student submitted documents similar to the one
below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.

Course Hero has millions of student submitted documents similar to the one below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.
542 CSE Advanced Data Structures and Algorithms Lab 1 Solution Jon Turner Part 1. The source code for this part appears below. Dheap.h (with unchanged parts omitted) ... class Dheap { public: ... // stats methods void clearStats(); string& stats2string(string&) const; private: ... // statistics counters int changekeyCount; int siftupCount; int siftdownCount; ... }; Dheap.cpp ... void Dheap::siftup(item i, int x) { int px = p(x); while (x > 1 && kee[i] < kee[h[px]]) { h[x] = h[px]; pos[h[x]] = x; x = px; px = p(x); siftupCount++; } h[x] = i; pos[i] = x; } ... void Dheap::siftdown(item i, int x) { int cx = minchild(x); while (cx != 0 && kee[h[cx]] < kee[i]) { h[x] = h[cx]; pos[h[x]] = x; x = cx; cx = minchild(x); siftdownCount += D; } h[x] = i; pos[i] = x; } ... void Dheap::changekey(item i, keytyp k) { changekeyCount++; -1- keytyp ki = kee[i]; kee[i] = k; if (k == ki) return; if (k < ki) siftup(i,pos[i]); else siftdown(i,pos[i]); } ... /** Clear the statistics counters */ void Dheap::clearStats() { siftupCount = siftdownCount = changekeyCount = 0; } /** Return a string representation of the statistics counters. * @param s is a string in which the result is returned * @return a reference to s */ string& Dheap::stats2string(string& s) const { string s1; s = "changekeyCount = " + Util::num2string(changekeyCount,s1) + " "; s += "siftupCount = " + Util::num2string(siftupCount,s1) + " "; s += "siftdownCount = " + Util::num2string(siftdownCount,s1); return s; } -2- Part 2. The table below shows the counter values, the values of d for each case, the maximum depth of the heap and the upper bounds on the counter values. 4K changekeyCount siftupCount siftdownCount d heap depth changekeyBound siftupBound siftdownBound 1,301 2,292 18,798 6 4 4,000 20,000 24,000 10K 2,213 1,790 29,592 12 3 10,000 33,000 36,000 20K 2,980 1,431 44,352 22 3 20,000 63,000 66,000 40K 3,575 1,225 75,852 42 2 40,000 82,000 84,000 100K 4,533 1,048 170,952 102 2 100,000 202,000 204,000 200K 5,165 882 297,950 202 2 200,000 402,000 404,000 400K 5,860 672 486,018 402 2 400,000 802,000 804,000 The changkeyBound is equal to the number of edges in the graph, the siftupBound is computed by multiplying ((# of inserts=1,000) + (the changekeyBound)) times the maximum heap depth and the siftdownBound is computed by multiplying the number of deletemin operations (1,000) times the product of d and the heap depth. The changekeyCount values are much smaller than the bounds. This apparently reflects the fact that when a new edge (u,v) is examined by Prim's algorithm, most of the time, the new edge is more expensive than cheap(v). This is a direct consequence of the fact that in the random graphs used here, the edge costs are random. In order to achieve the worst-case bound, the algorithm must examine the edges incident to a vertex in decreasing order of their cost, and this is not likely to occur when edge costs are random. We find that the examination of most edges does not trigger a call to changekey. The difference is quite striking. For the densest graphs, changekey is called for only about 1.5% of the edges. The siftupCount values are smaller than expected largely because they are directly dependent on the number of calls to changekey. However, we find that this only partly explains the difference, since the worst-case analysis would lead us to expect the number of siftup iterations to be equal to the number of changekeys times the depth of the heap. In fact, the number of siftup iterations is usually smaller than the number of changekeys, reflecting the fact that normally when we do a changekey, we end up not actually moving the item in the heap at all. The siftdownCount values behave in a way that is generally consistent with the worst-case bound. While they are a bit smaller than the bounds, they do grow in proportion with the bounds and are only smaller by a small constant factor. This reflects two factors. First, the siftdown is called once per deletemin and this number is n 1 for all graphs. It also appears that the number of iterations tends to more closely match the heap depth in this case than for the siftdowns. This makes sense, since we're always doing a siftdown from the top of the heap, and -3- its likely that the item being "sifted" will need to move most of the way down the heap in order to satisfy the heap-ordering condition. -4- Part 3. The table below shows the counter values, the values d of for each case, the maximum depth of the heap and the upper bounds on the counter values. 4K changekeyCount siftupCount siftdownCount d heap depth changekeyBound siftupBound siftdownBound 1301 5,751 14,056 2 9 4,000 36,000 18,000 10K 2,213 6,543 14,606 2 9 10,000 90,000 18,000 20K 2,980 7,205 14,674 2 9 20,000 180,000 18,000 40K 3,575 7,919 14,760 2 9 40,000 360,000 18,000 100K 4,533 8,504 14,760 2 9 100,000 900,000 18,000 200K 5,165 8,848 14,760 2 9 200,000 1,800,000 18,000 400K 5,860 9,427 14,768 2 9 400,000 3,600,000 18,000 In this case, the changekeyCount values are the same as before. There is some increase in the siftupCount values, since the smaller value of d makes it more likely that a changekey will move an item at least a few positions in the heap. The siftdownCounts are dramatically smaller in this case (as are the bounds) due to the smaller value of d. These results make it clear that for the random graphs used here, a choice of d=2 is better than a value that increases with the graph density. The dramatic reduction in the siftdownCounts should translate to a significant improvement in the running time for the densest graphs. Of course, the worst-case results remain valid, and it is certainly possible that there are some graphs for which we may be better off using a value of d that increases with the edge density. -5- Part 4. The source for the alternate version of Prim's algorithm appears below. /** Find a minimum spanning tree using Prim's algorithm. * This version uses a Fibonacci heap * @param wg is a weighted graph * @param mstree is a second weighted graph data structure in * which the result is to be returned; it is assumed to have * no edges */ void primF(Wgraph& wg, Wgraph& mstree) { vertex u,v; edge e; edge* cheap = new edge[wg.n()+1]; Fheaps nheap(wg.n()); fheap root; bool *inHeap = new bool[wg.n()+1]; //inHeap[u]=true if u in heap int numInHeap = 0; for (u = 1; u <= wg.n(); u++) inHeap[u] = false; e = wg.firstAt(1); if (e == 0) return; root = wg.mate(1,e); do { u = wg.mate(1,e); root = nheap.insert(u,root,wg.weight(e)); cheap[u] = e; inHeap[u] = true; numInHeap++; e = wg.nextAt(1,e); } while (e != 0); while (numInHeap > 0) { u = root; root = nheap.deletemin(root); inHeap[u] = false; numInHeap--; e = mstree.join( wg.left(cheap[u]), wg.right(cheap[u])); mstree.setWeight(e,wg.weight(cheap[u])); for (e = wg.firstAt(u); e != 0; e = wg.nextAt(u,e)) { v = wg.mate(u,e); if (inHeap[v] && wg.weight(e) < nheap.key(v)) { root = nheap.decreasekey(v,nheap.key(v) wg.weight(e),root); cheap[v] = e; } else if (!inHeap[v] && mstree.firstAt(v) == 0) { root = nheap.insert(v,root,wg.weight(e)); cheap[v] = e; inHeap[v] = true; numInHeap++; } } } delete [] cheap; delete [] inHeap; } -6- Part 5. The table below shows the average running times for the two versions of Prim's algorithm in milliseconds for graphs with 10,000 vertices and edge counts ranging from 20 K to 1M. This is averaged over 10 runs. 20K prim primF 5 10 50K 6 16 100K 16 25 150K 27 37 200K 39 48 300K 62 71 400K 84 97 500K 105 115 750K 159 169 1M 214 221 It is interesting to note that the version using Fibonacci heaps is consistently slower than the version using d-heaps. This most likely reflects the greater overhead associated with the F-heap data structure. The intrinsic simplicity of the d-heap gives it a significant "constant factor" advantage. It's also worth noting that even though the number edges in the graphs grows by a factor of 50, going from the smallest to the largest, the running time in the d-heaps case grows by a factor of 43 and the running time of the F-heaps case grows by a factor of 22. This is because not all of the running time grows as m grows. For the F-heap case, it appears that for the smallest graphs, the fraction that increases with m represents about half of the total. This reflects the fact that the number of changeKeys is so small that most of the growth in the running time comes just from the overhead of the inner loop, not the actual heap operations. For the d-heap case, the running time does increase almost as much as the number of edges. This suggests that the term that grows with m accounts for most of the running time even for the smallest graphs. Why is this, given that in both cases, the number of changeKey operations is small, so the cost associated with inner loop should be about the same in both cases? The reason is that the choice of d causes the running times of the deleteMins to increase with m. -7-
Find millions of documents on Course Hero - Study Guides, Lecture Notes, Reference Materials, Practice Exams and more. Course Hero has millions of course specific materials providing students with the best way to expand their education.

Below is a small sample set of documents:

Washington State - CSE - 542
CSE 542 Advanced Data Structures and AlgorithmsLab 2Jon Turner Due 2/15/2012Recall and follow the General notes from lab 1. This lab has two parts. In the first part you will be evaluating the performance of the augmenting path method for the max flow
Washington State - CSE - 542
CSE 542 Advanced Data Structures and AlgorithmsLab 2 SolutionJon TurnerPart 1. The source for the modified shortPath class appears below.. class shortPath : public augPath cfw_ public: shortPath(Flograph&amp;,int&amp;); private: bool findPath(); int int ; -#i
Washington State - CSE - 542
CSE 542 Advanced Data Structures and AlgorithmsLab 3Jon Turner Due 3/7/2012Recall and follow the General notes from lab 1. In this lab, you will be studying the performance of Kruskal's algorithm for the minimum spanning tree and the performance of the
Washington State - CSE - 542
Washington State - CSE - 542
Washington State - CSE - 542
2345678910111213141516
Washington State - CSE - 542
Washington State - CSE - 542
2345678910111213141516
Washington State - CSE - 542
Washington State - CSE - 542
Washington State - CSE - 542
Washington State - CSE - 542
Washington State - CSE - 542
Washington State - CSE - 542
Washington State - CSE - 542
Washington State - CSE - 542
Maintaining a PartitionJonathan Turner March 23, 2010This note is largely based on the analysis in Chapter 2 of Data Structures and Network Algorithms by Robert Tarjan, SIAM Press, 1985. The presentation has been expanded and adapted to provide a more s
Washington State - CSE - 542
Washington State - CSE - 542
Engineering Review SessionJon TurnerComputer Science &amp; Engineering Washington University www.arl.wustl.edu/~jstEngineering Big Picture Minimum Fibonacci Shortestspanning trees heaps general greedy method, Prim's algorithm, d-heaps how operations a
Washington State - CSE - 542
234567891011121314151617
Washington State - CSE - 542
Washington State - CSE - 542
Washington - H A&S - 222b
Energy and Environment H A&amp;S 220d Spring 2009 Peter Rhines Marcela Ewertwith Eric Lindahl, Bob Koon of the UW Geophysical Fluid Dynamics Laboratorythe suns ultraviolet (left) and infrared radiationimagers.gsfc.nasa.gov/ems/uv.html www.odysseymagazine.
Washington - H A&S - 222b
Lectures 2-3 week 1-2 2009: HAS222d Solar radiation, the greenhouse, global heat enginehttp:/en.wikipedia.org/The 4 streams of this course (see syllabus) 1.Energyforms of energy concetrated, diluted conservation transmission/movement transformation eff
Washington - H A&S - 222b
formed by subatomic particles: neutrons (neutral), protons (+), electrons () electrons move through the atomic volume, and are held there by the (VERY dense) nucleus The Atom= 1015 mHe= 1010 m the number of subatomic particles determines the propert
Washington - H A&S - 222b
Lectures 7-8 Thurs 23.iv.09 HAS 222d Introduction to energy &amp; environment Atmosphere-ocean: circulationThe atmosphere/ocean system is a `heat engine' largely driven by the sun.that is, a contraption in which heated fluid (air or water) expands and, under
Washington - H A&S - 222b
Lecture 9-10 HAS222d-09 Introduction to energy &amp; environment Air pollutionapplying flux and concentration ideas to pollution and to basic atmosphere/ocean dynamics The chapter in Spherical Cow on `steady state box models' assumes simple, well-mixed rese
Washington - H A&S - 222b
Arctic sea ice animation (Tom Agnew, Environment Canada)Lecture 12 HAS222d Intro to energy and environment 2009 slides on water in the atmosphere P.B. RhinesSatellite image of water vapor (not cloud.it's gaseous water which is normally invisible). This
Washington - H A&S - 222b
HAS222d 2009 Intro to Energy and Environment: Lecture 13: Global energy: national profiles, local solutionsWe want to look into the global use of energy by humans, estimated at 4 x 1020 J/year. This is the number which, divided by the global population a
Washington - H A&S - 222b
HA&amp;S 222d Spr 2009 Lecture 14 slides Arctic history and climateDeep timeshowing the cooling of the Earth since the end of the Creaceous period (the dinosaur era). There was little or no snow or ice on Earth then. Abruptly, about 2.5 million years ago, t
Washington - H A&S - 222b
Lecture 15 The Necklace around the Arctic Arctic indigenous peoples and ANWR in AlaskaThere are many sources of Arctic literature; one readable book on the changing Arctic (with emphasis on climate change) is the Arctic Climate Impact Assessment, availab
Washington - H A&S - 222b
HAS 222d-2009 Lecture 16 week 9 Exponential growth P.RhinesDigression on exponential growth and global population: exponential growth occurs when the growth is proportional to the population.for example, if each person has the same number of children dur
Washington - H A&S - 222b
HAS222d Intro to Energy and Environement: Lecture 17 week 9 Solutions40% off energy use in US goes into generating electricity generation efficiency: 33% electric power loss: plant to consumer 7.2% in US.costs more than shipping the fossil fuel US averag
Washington - H A&S - 222b
1H A&amp;S 222d Introduction to Energy and Environment P.B.Rhines Spring 2009 Lectures 2 and 3 some changes appear in RED Notes on SOLAR RADIATION and the EARTHS GREENHOUSESuns radiation and its spectrum. We introduced the suns energy, and the spectrum of w
Washington - H A&S - 222b
1 H A&amp;S 222d Introduction to Energy and Environment P. B.Rhines spring 2009 Lecture 4I. INTRODUCTION TO HUMAN ENERGETICS We will encounter several very different views of the environment: McNeill's history (which has considerable sociology and philosophy
Washington - H A&S - 222b
1 H A&amp;S 222d Introduction to Energy and Environment: Life Under the Pale Sun P.B.Rhines, M Ewert Spring 2009 Lecture 5 THERMAL ENERGYSCIENCE CORE: PHYSICS OF ENERGY, continued We are beginning to fill in the list of `forms of energy' and to give examples
Washington - H A&S - 222b
Peak Oil and Climate ChangeAS222d 20 May 2009 284 Mary Gates James W. Murray School of Oceanography University of Washington1999 - ~$10/b2008 - ~$147/bIEA World Energy Outlook - Nov 2008 (NEW) &quot;The world's energy system is at a crossroads. Current glo
Washington - H A&S - 222b
1H A&amp;S 222a Energy and Environment: Life Under the Pale Sun P.B. Rhines, Marcela Ewert 1 June 2009 REVIEW: There are 4 sub-regions in this course: 1. energy in nature, 2. the global environment 3. human energy use 4. life and energy at the rim of the Arc
Memorial University - PHYSICS - 1051
Memorial University - PHYSICS - 1051
Memorial University - PHYSICS - 1051
Memorial University - PHYSICS - 1051
Memorial University - PHYSICS - 1051
Memorial University - PHYSICS - 1051
Memorial University - PHYSICS - 1051
Memorial University - PHYSICS - 1051
Memorial University - PHYSICS - 1051
Memorial University - PHYSICS - 1051
Memorial University - PHYSICS - 1051
Memorial University - PHYSICS - 1051
Memorial University - PHYSICS - 1051
Memorial University - PHYSICS - 1051
Memorial University - PHYSICS - 1051
Memorial University - PHYSICS - 1051
Memorial University - PHYSICS - 1051
Memorial University - PHYSICS - 1051
Physics 1051, Winter 2012 Sections 4, 5, &amp; 6 INFORMATION: Website: access from http:/www.mun.ca/physics/undergraduates/course_webpages.php OR Desire2Learn o Outline, schedule, evaluation, lecture notes, workshops, problems sets Desire2Learn o Announcement
Memorial University - PHYSICS - 1051
Oscillations: Review (Chapter 12) Oscillations: motions that are periodic in time (i.e. repetitive) o Swinging object (pendulum) o Vibrating object (spring, guitar string, etc.) o Part of medium (i.e. string, water) as wave passes by Oscillation requires
Memorial University - PHYSICS - 1051
Simple and Physical Pendula Simple Pendulum: &quot;point&quot; mass m on massless string L How do we find natural frequency ? o Use Newton's 2nd law to get differential equation for SHM o Identify from differential equation Forces on hanging mass: o For string at
Memorial University - PHYSICS - 1051
Waves transport energy: Mechanical waves: o propagating disturbance (from equilibrium) of medium Electromagnetic waves (i.e. light, radio, etc.): o Propagate without mediumE BMECHANICAL WAVES (Chapter 13) Concepts to watch for: o Types of waves: transve
Memorial University - PHYSICS - 1051
Sinusoidal Traveling Waves (13.3 in text) For any traveling wave, expect y (x, t ) = f (x - vt ) o Ask: what is the form of function f for a sinusoidal wave with: specific speed v specific wavelength First: look at x dependence of sinusoidal wave function
Memorial University - PHYSICS - 1051
Transverse Wave on a String Wave speed depends on properties of medium: For a string, will see v = T / o T is string tension o is mass/unit length (i.e. linear density) to see, apply 2nd law to element of string (red) in frame moving left with crest at s
Memorial University - PHYSICS - 1051
SOUND (Longitudinal) WAVES Imagine &quot;planes&quot; of atoms in tube of air As sound wave passes, each plane oscillates along direction of propagation Can represent as displacement of air or pressure change (90 out of phase)Wave function for sound Can write in t