Lab1 - 4 Slides Per Page

download Lab1 - 4 Slides Per Page

of 9

Transcript of Lab1 - 4 Slides Per Page

  • 8/10/2019 Lab1 - 4 Slides Per Page

    1/9

    Programming TechniquesLab 1

    Lecture Overview

    Note on invariants

    The Dutch national flag problem

    Star proximity

    Profiling

    Debugging

    nvariants

    The assert! " function can be #isable# #uringcompiling so you shoul# never put comman#s thatare part of the algorithm in the assertion$$$

    Programmers can eliminate the assertions withoutchanging the source co#e% if the macro ND&'()is#efine# before the inclusion of *assert+h,- or thesource co#e is compile# with the .DND&'()flag theassert!" macro is #efine# simply as%

    /#efine assert!ignore"!!voi#" 0"

    e+g+ assert! myPointer malloc!si2eof3myPointer "4 55bro6enco#e

    Problem Statement

    7ssume we have an unor#ere# array withre#an# blueelements

    8ow #o we separate the re# from the blueelements

    9emember re#,* blue

  • 8/10/2019 Lab1 - 4 Slides Per Page

    2/9

    The #utch national flag problem

    The #utch national flag problem !1st attempt":ount an# reconstruct

    /of%9e#%'lue%

    The #utch national flag problem !1st attempt":ount an# reconstruct

    /of%9e#% ;'lue% ;

    The #utch national flag problem !1st attempt"

    :reate a new array with the number of re# an#blue elements counte# before

  • 8/10/2019 Lab1 - 4 Slides Per Page

    3/9

    The #utch national flag problem !

  • 8/10/2019 Lab1 - 4 Slides Per Page

    4/9

    The #utch national flag problem !

  • 8/10/2019 Lab1 - 4 Slides Per Page

    5/9

    The #utch national flag problem

    =hich approach ren#ers better results> =hat is the complexity of this problem> n what #irections can we exam the problem>

    Optimi2ations might complicate things

    Same experiment run with optimi2ation #isable# !left" an# with optimi2ation !.O?flag" enable# !right"+ The re# @ blue is represente# by 0 an# 1- respectively- in anarray of integers+

    10

  • 8/10/2019 Lab1 - 4 Slides Per Page

    6/9

    The #utch national flag problem !original"

    Problem invariants%7ll the cells to the left of re# mar6er are re#$7ll the cells to the right of blue mar6er are blue$

    9ules of the game

    Start transparent mar6er+

    f re#element is foun#- swap the element pointe# by the transparentmar6er with the one pointe# by the re#one an# increment bothmar6ers+

    f a white element is foun#- increment only the transparent mar6er+ f a blueelement is foun#- swap the element pointe# by the transparent

    mar6er with the one pointe# by the blueone an# #ecrement the bluemar6er only+

    The #utch national flag problem !original"

    The #utch national flag problem is cre#ite# to DiG6stra+ t is the i#ea behin# the ?.way partitioning in quic6.sort Huic6sort goes qua#raticunless partitioning stops on equal 6eys+

    Star proximity

    Task: Find all the pairs ofpoints that are closer thana given distance d, (d

  • 8/10/2019 Lab1 - 4 Slides Per Page

    7/9

    Star proximity !brute force"

    We could compare the distanceof every pair of points i, j usingthe knon formula:

    !ut, e ill end up ith a vastamount of comparisons" #incee have Ndots and e compareeach one of them ith theremaining N-1 dots, e end upith N*(N-1)/2 ~ O(N2)calculations. $his pro%lem%ecomes infeasi%lefor large N.

    d i, j=(xix j)2+(yiyj)

    2

    Star proximity !

  • 8/10/2019 Lab1 - 4 Slides Per Page

    8/9

    A+0

  • 8/10/2019 Lab1 - 4 Slides Per Page

    9/9

    Star proximity

    intmain(intargc, charargv23)4 inti, j, 0 atoi(argv213)5 d atof(argv23)5

    6 17d5 grid mallocd(68, 68)5 for(i 5 i < 685 i88) for(j 5 j < 685 j88) grid2i32j3 095 for(i 5 i < 05 i88) gridinsert(randFloat(), randFloat())5 printf(;dedges shorter than f=n;, cnt, d)5 return5>

    ?include