480 likes | 625 Views
CS 24 – Problem Solving II (Data Structures). Diana Franklin. Course Objectives. Become a programmer More mature through practice Pointers, linked structures Recursion Data structures. Course Objectives. Manage Complexity – Complex systems change the world
E N D
CS 24 – Problem Solving II (Data Structures) Diana Franklin
Course Objectives • Become a programmer • More mature through practice • Pointers, linked structures • Recursion • Data structures
Course Objectives • Manage Complexity – Complex systems change the world • Analysis to make decisions on algorithms • Choose proper data structures • Learn object-oriented programming
Code Complexity • Google, Google, Google – how has it changed the world? • Hundreds of programs execute for one query • Millions of queries each day • Web crawlers gathering data • E-mails being read and sent • Photos in Google+ and Picasa • Everything is connected
Big Data, Genomics, and Cancer • 2010: Complete genomic characterization of a rare cancer tumor of one patient helped in clinical decision making and in identifying therapeutic approaches where no established treatment protocols existed • 1st use of genomics to inform choice of cancer treatment options, stabilize cancer • 34 researchers kept patient alive 6 months [1] Jones et al. (British Columbia Cancer Center): Evolution of an adenocarcinoma in response to selection by targeted kinase inhibitors. Genome Biology 2010 11:R82.
Big Data, Genomics, and Cancer • Circos graph to compare genomes • CNV: chromosomal copy number variation • LOH: loss of heterozygosity • T1 pre-treatment tumor cell • T2 post-treatment tumor cell • Δ degree of change between 2 cancers • R normal cell Large regions of Chromosomal loss
Big Data, Genomics, and Cancer • Led to hypothesis of mechanism driving tumor and drugs to target it • Clinic applied drug not approved for disease • Shrank tumors by 22%, no new lesions • Not cure, but stabilized rare cancer Could we economically scale up from 34 researchers/1 patient in 3 months to a million patients per year?
Ways to optimize programs • faster • easy for programmer to understand • easy for user to understand • as little memory usage as possible • modular code – clear code & faster programming, reusability • scalability (throughput) • well organized • correctness • compatibility
Getting a taste • Algorithms to find item in the phone book • Ways to store information • Choices: List, Tree, Graph • Google+ Circles • LA Freeway system • Management Structure of an Organization
Searching for a number in a phone book that has n numbers in it • Start at the beginning • Best case - • Average case – • Worst case -
Searching for a number in a phone book that has n numbers in it • Start at the beginning • while (we haven’t found it) • advance to the next one • Best case - 1 • Average case – n/2 • Worst case - n
Searching for a number in a sorted phone book • Look at the middle element • best-case - • worst-case • average-case –
Searching for a number in a sorted phone book • Look at the middle element • if it’s smaller than that one, throw out top half • if it’s larger than that one, throw out bottom half • repeat until we have only one item left • best-case - • worst-case • average-case –
Searching for a number in a sorted phone book • Look at the middle element • if it’s smaller than that one, throw out top half • if it’s larger than that one, throw out bottom half • repeat until we have only one item left • best-case - 1 • worst-case log2(n) • average-case –summation of: time*probability of that time • 1*1/n + 2*2/n + 3*4/n + 4*8/n+…+log2(n)*n/2
n*1/2*1/2*1/2*… = 1 • n*(1/2)^j = 1 • 1/(2^j) = 1/n • n = 2^j • log2(n) = j
Spiral Approach • Present the same topic multiple times • More depth the second time • Presented by a different person • Reinforce earlier learning while encouraging new perspective
Spiral Approach • Reinforced topics • Arrays, File I/O, Command-Line Args • Pointers, Recursion • New topics, deep coverage • Linked structures (lists, queues, trees) • Classes • New topics, shallow coverage • advanced object-oriented concepts, complexity
Why so many languages? • Python – candy – bribe students with flashy, simple applications. Hide details • C – veggies – Expose details. Mastery necessary for OS, networks, compilers, etc. • C++ - fruit – Introduce objects as programs get more complex.
Assignment Philosophy • Success is based on what you DO, not what you ARE • College requires better work habits than in high school, upper-division more than lower • Instill / Encourage / Reward good habits in lower division • Give more freedom in upper division
http://www.cs.ucsb.edu/~franklin/24/ • Syllabus • Assignments (with dates) • Lecture resources • Definition of plagiarism – never work in groups of 3+ • Detailed grading breakdown • Late policy – 24 hours, 10% off • Information guides for gdb, compiling, unix • etc, etc, etc.
Logistics • Labs – attendance mandatory • Mini-Exams – 2 – in between to make sure I don’t lose you • Exams – 2 midterms, 1 final • Programs – Some individual, some pair programming • Participation Extra Credit – asking / answering questions in class
Lab tomorrow • If you have a CoE account, sit at a computer right away • If you do not have a CoE account, join someone who is already at a computer • You may switch partners in later labs once everyone has accounts
Waiting List • Everyone on the waiting list who had the pre-reqs is making it in the class • The class is completely, completely full (slightly overenrolled) with those students • benji@cs.ucsb.edu, franklin@cs.ucsb.edu
Arrays • How is an array different from a pointer? • int *x • int x[10]; • What does this declare? • int *x[10]; • int **x;
Arrays vs Pointers • How do you access arrays vs pointers? • int x[10]; • int *y = (int *)malloc(sizeof(int)*10); • They are mostly the same: • x[5] = 10; y[5] = 10; • *(x+5) = 10; *(y+5) = 10; • The big differences: • y = whatever; // can’t do x = anything • free(y); // don’t free x
Strings • How are strings stored in C? • How do you declare an array of strings?
Strings • How are strings stored in C? • array of characters • end with a hidden ‘\0’ • How do you declare an array of strings?
Strings • How are strings stored in C? • array of characters • end with a hidden ‘\0’ • How do you declare an array of strings? • char **strings; • char *strings[10];
Pointer Review • General knowledge • Drawing the memory • Reading and Writing
What do you know about pointersthat was covered in CS 16? • They store an address in a variable • You can have a pointer pointing to another pointer • You can change values in arrays more easily • There are void pointers, and you need to cast them if you want to dereference them • Pointers are used for dynamic memory allocation
Draw the memory of each int *x, *y; x = &a; y = &b; x = y; int a, b; a = 5; b = 3; a = b; What is the difference between (a = b) and (x = y) for what the computer does? What is the difference between (a = b) and (x = y) for how we view them? The way to make working with pointers easy is to operate on them mechanically like the computer. They are no different than integers.
Pointer Review – what prints out? int x = 3, y = 4; int *pX = &x, *pY = &y; (pX == pY) ? Print(“Same\n”): print(“Not Same\n”); (*pX == *pY) ? Print(“Equal\n”) : print(“NEqual\n”); x = y (pX == pY) ? Print(“Same\n”): print(“Not Same\n”); (*pX == *pY) ? Print(“Equal\n”) : print(“NEqual\n”); x = 10; pX = pY; (pX == pY) ? Print(“Same\n”): print(“Not Same\n”); (*pX == *pY) ? Print(“Equal\n”) : print(“NEqual\n”);
Structs • What do you know about structs? • member variables or fields • you can have structs inside of structs inside of structs… • organizes your data • composite data type • you can put them in an array (just like any other type) • create many variables of that type
Structs • What do you know about structs? • You can create your own data type • Make many copies of that data type • dynamically allocate structs • Variables with multiple fields – programmer defines what the fields hold
Structs typedefstructblah { struct blah { int weight; int weight; floatprice; float price; } fazizzle; }; typedefintfjlsajfjaeisje How do I declare three variables, named apple, banana, and melon, of type struct blah or fazizzle? structblah apple, banana, melon; fazizzleorange, pineapple, kiwi; apple.weight = 5;
Structs typedefstruct _product { int weight; floatprice; } product; How do I declare three variables, named apple, banana, and melon, of type product? struct _product apple, banana, melon; product apple, banana, melon; apple.weight = 5;
Structs typedefstruct _product { int weight; floatprice; } product; product apple; product banana, melon;
Structs typedefstruct _product { int weight; floatprice; } product; product apple, banana, melon; How do I set the weight field of apple?
Structs typedefstruct _product { int weight; floatprice; } product; product apple, banana, melon; product *pApple, *pBanana, *pMelon; pApple = &apple; pBanana = &banana; pMelon = & melon; How do I set the weight field of apple? apple.weight = 5; pApple->weight = 5; (*pApple).weight = 5;
Structs typedefstruct _product { int*weight; floatprice; } product; product apple, banana, melon; product *pApple, *pBanana, *pMelon; pApple = &apple; pBanana = &banana; pMelon = & melon; How do I set the weight field of apple? *(apple.weight) = 5; apple.weight[10] = 5 *(pApple->weight) = 5; (*pApple).weight = 5;
When should we use pointers instead of non-pointers? • If you have a local variable you want to use, but the memory has already been allocated • Dynamic size: If you want to be to vary size OR you don’t know how big it is going to be • Time: You don’t want to copy the whole piece of data. Just pass a pointer to it. • Accessibility: You want the function being called to be able to change values in the caller’s memory • Lifetime: You want to create something that lives past the end of the function call’s life. You create it and pass its address out of the function. • Pointers can change around: you have a “label” or variable that pointers to different things through time, but those things never get destroyed.
Linking pointers Typdefstruct_node{ int value; struct_node*next; } node; node l1, l2, l3, l4; node*p1, *p2, *p3, *p4; p1 = &l1; p2 = &l2; p3 = &l3; p4 = &l4; l1.value = 1; l2.value = 2; l3.value = 4; l4.value = 8; p1->next = p2; p2->next = p3; p3->next = p4; p4->next = p1; p1->value = 5; p1->next->value = 3; p1->next->next->value = 7; for(i=0;i<4;i++) { printf(“%d, “,p2->value); p2 = p2->next; } p2 = null; p3 = null; p4 = null;