




























































































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
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Problem Solving with Algorithms and data Structures in java Programming Language.From the university of Auckland New Zealand.
Typology: Exercises
1 / 240
This page cannot be seen from the preview
Don't miss anything!
ONE
INTRODUCTION
1.1 Objectives
1.2 Getting Started
The way we think about programming has undergone many changes in the years since the first electronic computers required patch cables and switches to convey instructions from human to machine. As is the case with many aspects of society, changes in computing technology provide computer scientists with a growing number of tools and platforms on which to practice their craft. Advances such as faster processors, high-speed networks, and large memory ca- pacities have created a spiral of complexity through which computer scientists must navigate. Throughout all of this rapid evolution, a number of basic principles have remained constant. The science of computing is concerned with using computers to solve problems.
You have no doubt spent considerable time learning the basics of problem-solving and hope- fully feel confident in your ability to take a problem statement and develop a solution. You have also learned that writing computer programs is often hard. The complexity of large problems and the corresponding complexity of the solutions can tend to overshadow the fundamental ideas related to the problem-solving process.
This chapter emphasizes two important areas for the rest of the text. First, it reviews the frame- work within which computer science and the study of algorithms and data structures must fit, in particular, the reasons why we need to study these topics and how understanding these top- ics helps us to become better problem solvers. Second, we review the Python programming language. Although we cannot provide a detailed, exhaustive reference, we will give examples and explanations for the basic constructs and ideas that will occur throughout the remaining chapters.
1.3 What Is Computer Science?
Computer science is often difficult to define. This is probably due to the unfortunate use of the word “computer” in the name. As you are perhaps aware, computer science is not simply the study of computers. Although computers play an important supporting role as a tool in the discipline, they are just that – tools.
Computer science is the study of problems, problem-solving, and the solutions that come out of the problem-solving process. Given a problem, a computer scientist’s goal is to develop an algorithm, a step-by-step list of instructions for solving any instance of the problem that might arise. Algorithms are finite processes that if followed will solve the problem. Algorithms are solutions.
Computer science can be thought of as the study of algorithms. However, we must be careful to include the fact that some problems may not have a solution. Although proving this statement is beyond the scope of this text, the fact that some problems cannot be solved is important for those who study computer science. We can fully define computer science, then, by including both types of problems and stating that computer science is the study of solutions to problems as well as the study of problems with no solutions.
It is also very common to include the word computable when describing problems and solu- tions. We say that a problem is computable if an algorithm exists for solving it. An alternative definition for computer science, then, is to say that computer science is the study of problems that are and that are not computable, the study of the existence and the nonexistence of algo- rithms. In any case, you will note that the word “computer” did not come up at all. Solutions are considered independent from the machine.
Computer science, as it pertains to the problem-solving process itself, is also the study of abstraction. Abstraction allows us to view the problem and solution in such a way as to separate the so-called logical and physical perspectives. The basic idea is familiar to us in a common example.
Consider the automobile that you may have driven to school or work today. As a driver, a user of the car, you have certain interactions that take place in order to utilize the car for its intended purpose. You get in, insert the key, start the car, shift, brake, accelerate, and steer in order to drive. From an abstraction point of view, we can say that you are seeing the logical perspective of the automobile. You are using the functions provided by the car designers for the purpose of transporting you from one location to another. These functions are sometimes also referred to as the interface.
On the other hand, the mechanic who must repair your automobile takes a very different point of view. She not only knows how to drive but must know all of the details necessary to carry out all the functions that we take for granted. She needs to understand how the engine works, how the transmission shifts gears, how temperature is controlled, and so on. This is known as the physical perspective, the details that take place “under the hood.”
The same thing happens when we use computers. Most people use computers to write docu- ments, send and receive email, surf the web, play music, store images, and play games without any knowledge of the details that take place to allow those types of applications to work. They view computers from a logical or user perspective. Computer scientists, programmers, technol- ogy support staff, and system administrators take a very different view of the computer. They
4 Chapter 1. Introduction
Control constructs allow algorithmic steps to be represented in a convenient yet unambiguous way. At a minimum, algorithms require constructs that perform sequential processing, selection for decision-making, and iteration for repetitive control. As long as the language provides these basic statements, it can be used for algorithm representation.
All data items in the computer are represented as strings of binary digits. In order to give these strings meaning, we need to have data types. Data types provide an interpretation for this binary data so that we can think about the data in terms that make sense with respect to the problem being solved. These low-level, built-in data types (sometimes called the primitive data types) provide the building blocks for algorithm development.
For example, most programming languages provide a data type for integers. Strings of binary digits in the computer’s memory can be interpreted as integers and given the typical meanings that we commonly associate with integers (e.g. 23 , 654 , and − 19 ). In addition, a data type also provides a description of the operations that the data items can participate in. With integers, operations such as addition, subtraction, and multiplication are common. We have come to expect that numeric types of data can participate in these arithmetic operations.
The difficulty that often arises for us is the fact that problems and their solutions are very complex. These simple, language-provided constructs and data types, although certainly suf- ficient to represent complex solutions, are typically at a disadvantage as we work through the problem-solving process. We need ways to control this complexity and assist with the creation of solutions.
To manage the complexity of problems and the problem-solving process, computer scientists use abstractions to allow them to focus on the “big picture” without getting lost in the details. By creating models of the problem domain, we are able to utilize a better and more efficient problem-solving process. These models allow us to describe the data that our algorithms will manipulate in a much more consistent way with respect to the problem itself.
Earlier, we referred to procedural abstraction as a process that hides the details of a particular function to allow the user or client to view it at a very high level. We now turn our attention to a similar idea, that of data abstraction. An abstract data type, sometimes called an ADT, is a logical description of how we view the data and the operations that are allowed without regard to how they will be implemented. This means that we are concerned only with what the data is representing and not with how it will eventually be constructed. By providing this level of abstraction, we are creating an encapsulation around the data. The idea is that by encapsulating the details of the implementation, we are hiding them from the user’s view. This is called information hiding.
Figure 1.2 shows a picture of what an abstract data type is and how it operates. The user interacts with the interface, using the operations that have been specified by the abstract data type. The abstract data type is the shell that the user interacts with. The implementation is hidden one level deeper. The user is not concerned with the details of the implementation.
The implementation of an abstract data type, often referred to as a data structure, will require that we provide a physical view of the data using some collection of programming constructs and primitive data types. As we discussed earlier, the separation of these two perspectives will
6 Chapter 1. Introduction
Figure 1.2: Abstract Data Type
allow us to define the complex data models for our problems without giving any indication as to the details of how the model will actually be built. This provides an implementation- independent view of the data. Since there will usually be many different ways to implement an abstract data type, this implementation independence allows the programmer to switch the details of the implementation without changing the way the user of the data interacts with it. The user can remain focused on the problem-solving process.
Computer scientists learn by experience. We learn by seeing others solve problems and by solving problems by ourselves. Being exposed to different problem-solving techniques and seeing how different algorithms are designed helps us to take on the next challenging problem that we are given. By considering a number of different algorithms, we can begin to develop pattern recognition so that the next time a similar problem arises, we are better able to solve it.
Algorithms are often quite different from one another. Consider the example of sqrt seen earlier. It is entirely possible that there are many different ways to implement the details to compute the square root function. One algorithm may use many fewer resources than another. One algorithm might take 10 times as long to return the result as the other. We would like to have some way to compare these two solutions. Even though they both work, one is perhaps “better” than the other. We might suggest that one is more efficient or that one simply works faster or uses less memory. As we study algorithms, we can learn analysis techniques that allow us to compare and contrast solutions based solely on their own characteristics, not the characteristics of the program or computer used to implement them.
In the worst case scenario, we may have a problem that is intractable, meaning that there is no algorithm that can solve the problem in a realistic amount of time. It is important to be able to distinguish between those problems that have solutions, those that do not, and those where solutions exist but require too much time or other resources to work reasonably.
There will often be trade-offs that we will need to identify and decide upon. As computer scientists, in addition to our ability to solve problems, we will also need to know and understand
1.3. What Is Computer Science? 7
Operation Name Operator Explanation less than < Less than operator greater than > Greater than operator less than or equal <= Less than or equal to operator greater than or equal >= Greater than or equal to operator equal == Equality operator not equal =! Not equal operator logical and and Both operands True for result to be True logical or or Either operand True for result to be True logical not not Negates the truth value: False becomes True, True becomes False
Table 1.1: Relational and Logical Operators
print(2+34) # print((2+3)4) # print(210) # print(6/3) #2. print(7/3) #2. print(7//3) # print(7%3) # print(3/6) #0. print(3//6) # print(3%6) # print(2100) # 1267650600228229401496703205376
The boolean data type, implemented as the Python bool class, will be quite useful for representing truth values. The possible state values for a boolean object are True and False with the standard boolean operators, and , or , and not.
True True False False False or True True not (False or True) False True and True True
Boolean data objects are also used as results for comparison operators such as equality (==) and greater than (>). In addition, relational operators and logical operators can be combined together to form complex logical questions. Table 1.1 shows the relational and logical operators with examples shown in the session that follows.
print(5 == 10) print(10 > 5)
1.4. Review of Basic Python 9
Figure 1.3: Variables Hold References to Data Objects
Figure 1.4: Assignment changes the Reference
print((5 >= 1) and (5 <= 10))
Identifiers are used in programming languages as names. In Python, identifiers start with a letter or an underscore (_), are case sensitive, and can be of any length. Remember that it is always a good idea to use names that convey meaning so that your program code is easier to read and understand.
A Python variable is created when a name is used for the first time on the left-hand side of an assignment statement. Assignment statements provide a way to associate a name with a value. The variable will hold a reference to a piece of data and not the data itself. Consider the following session:
the_sum = 0 the_sum 0 the_sum = the_sum + 1 the_sum 1 the_sum = True the_sum True
The assignment statement the_sum = 0 creates a variable called the_sum and lets it hold the reference to the data object 0 (see Figure 1.3). In general, the right-hand side of the assignment statement is evaluated and a reference to the resulting data object is “assigned” to the name on the left-hand side. At this point in our example, the type of the variable is integer as that is the type of the data currently being referred to by “the_sum.” If the type of the data changes (see Figure 1.4), as shown above with the boolean value True, so does the type of the variable ( the_sum is now of the type boolean). The assignment statement changes the reference being held by the variable. This is a dynamic characteristic of Python. The same variable can refer to many different types of data.
10 Chapter 1. Introduction
Method Name Use Explanation append a_list.append(item) Adds a new item to the end of a list insert a_list.insert(i,item) Inserts an item at the 𝑖th^ position in a list pop a_list.pop() Removes and returns the last item in a list pop a_list.pop(i) Removes and returns the 𝑖th^ item in a list sort a_list.sort() Modifies a list to be sorted reverse a_list.reverse() Modifies a list to be in reverse order del del a_list[i] Deletes the item in the 𝑖th^ position index a_list.index(item) Returns the index of the first occurrence of item count a_list.count(item) Returns the number of occurrences of item remove a_list.remove(item) Removes the first occurrence of item
Table 1.3: Methods Provided by Lists in Python
following session:
my_list = [1,2,3,4] A = [my_list]* print(A) my_list[2]= print(A)
The variable A holds a collection of three references to the original list called my_list. Note that a change to one element of my_list shows up in all three occurrences in A.
Lists support a number of methods that will be used to build data structures. Table 1.3 provides a summary. Examples of their use follow.
my_list = [1024, 3, True, 6.5] my_list.append(False) print(my_list) my_list.insert(2,4.5) print(my_list) print(my_list.pop()) print(my_list) print(my_list.pop(1)) print(my_list) my_list.pop(2) print(my_list) my_list.sort() print(my_list) my_list.reverse() print(my_list) print(my_list.count(6.5)) print(my_list.index(4.5)) my_list.remove(6.5) print(my_list) del my_list[0] print(my_list)
12 Chapter 1. Introduction
You can see that some of the methods, such as pop , return a value and also modify the list. Others, such as reverse , simply modify the list with no return value. pop will default to the end of the list but can also remove and return a specific item. The index range starting from 0 is again used for these methods. You should also notice the familiar “dot” notation for asking an object to invoke a method. my_list.append(False) can be read as “ask the object my_list to perform its append method and send it the value False .” Even simple data objects such as integers can invoke methods in this way.
(54).add(21) 75
In this fragment we are asking the integer object 54 to execute its add method (called add in Python) and passing it 21 as the value to add. The result is the sum, 75. Of course, we usually write this as 54 + 21. We will say much more about these methods later in this section.
One common Python function that is often discussed in conjunction with lists is the range function. range produces a range object that represents a sequence of values. By using the list function, it is possible to see the value of the range object as a list. This is illustrated below.
range(10) range(0, 10) list(range(10)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] range(5,10) range(5, 10) list(range(5,10)) [5, 6, 7, 8, 9] list(range(5,10,2)) [5, 7, 9] list(range(10,1,-1)) [10, 9, 8, 7, 6, 5, 4, 3, 2]
The range object represents a sequence of integers. By default, it will start with 0. If you provide more parameters, it will start and end at particular points and can even skip items. In our first example, range(10) , the sequence starts with 0 and goes up to but does not include
Strings are sequential collections of zero or more letters, numbers and other symbols. We call these letters, numbers and other symbols characters. Literal string values are differentiated from identifiers by using quotation marks (either single or double).
"David" 'David' my_name = "David" my_name[3]
1.4. Review of Basic Python 13
my_list [1, 3, True, 6.5] my_list[0]=2** my_list [1024, 3, True, 6.5] my_name 'David' my_name[0]='X' Traceback (most recent call last): File "<pyshell#84>", line 1, in
my_name[0]='X' TypeError: 'str' object does not support item assignment
Note that the error (or traceback) message displayed above is obtained on a Mac OS X machine. If you are running the above code snippet on a Windows machine, your error output will more likely be as follows.
my_name[0]='X' Traceback (most recent call last): File "<pyshell#84>", line 1, in -toplevel- my_name[0]='X' TypeError: object doesn't support item assignment
Depending on your operating system, or version of Python, the output may slightly vary. How- ever it will still indicate where and what the error is. You may want to experiment for yourself and get acquainted with the error message for easier and faster debugging. For the remainder of this work, we will only display the Mac OS X error messages.
Tuples are very similar to lists in that they are heterogeneous sequences of data. The difference is that a tuple is immutable, like a string. A tuple cannot be changed. Tuples are written as comma-delimited values enclosed in parentheses. As sequences, they can use any operation described above. For example,
my_tuple = (2,True,4.96) my_tuple (2, True, 4.96) len(my_tuple)
my_list [1024, 3, True, 6.5] >>> my_name 'David' >>> my_name[0]='X' Traceback (most recent call last): File "<pyshell#84>", line 1, in
my_name[0]='X' TypeError: 'str' object does not support item assignment >>> Note that the error (or traceback) message displayed above is obtained on a Mac OS X machine. If you are running the above code snippet on a Windows machine, your error output will more likely be as follows. >>> my_name[0]='X' Traceback (most recent call last): File "<pyshell#84>", line 1, in -toplevel- my_name[0]='X' TypeError: object doesn't support item assignment >>> Depending on your operating system, or version of Python, the output may slightly vary. How- ever it will still indicate where and what the error is. You may want to experiment for yourself and get acquainted with the error message for easier and faster debugging. For the remainder of this work, we will only display the Mac OS X error messages. Tuples are very similar to lists in that they are heterogeneous sequences of data. The difference is that a tuple is immutable, like a string. A tuple cannot be changed. Tuples are written as comma-delimited values enclosed in parentheses. As sequences, they can use any operation described above. For example, >>> my_tuple = (2,True,4.96) >>> my_tuple (2, True, 4.96) >>> len(my_tuple) 3 my_tuple[0] 2 my_tuple (^) * 3 (2, True, 4.96, 2, True, 4.96, 2, True, 4.96) my_tuple[0:2] (2, True)
However, if you try to change an item in a tuple, you will get an error. Note that the error message provides location and reason for the problem.
1.4. Review of Basic Python 15
Operator Use Explanation in x. in ( set ) Set membership len len ( set ) Returns the cardinality (i.e. the length) of the set | set1 | set2 Returns a new set with all elements from both sets & set1 & set2 Returns a new set with only the elements common to both sets
Table 1.5: Operations on a Set in Python
my_tuple[1]=False Traceback (most recent call last): File "<pyshell#137>", line 1, in
my_tuple[1]=False TypeError: 'tuple' object does not support item assignment
A set is an unordered collection of zero or more immutable Python data objects. Sets do not allow duplicates and are written as comma-delimited values enclosed in curly braces. The empty set is represented by set(). Sets are heterogeneous, and the collection can be assigned to a variable as below.
{3,6,"cat",4.5,False} {False, 4.5, 3, 6, 'cat'} my_set = {3,6,"cat",4.5,False} my_set {False, 3, 4.5, 6, 'cat'}
Even though sets are not considered to be sequential, they do support a few of the familiar operations presented earlier. Table 1.5 reviews these operations and the following session gives examples of their use.
my_set {False, 3, 4.5, 6, 'cat'} len(my_set)
5
False in my_set True "dog" in my_set False
Sets support a number of methods that should be familiar to those who have worked with them in a mathematics setting. Table 1.6 provides a summary. Examples of their use follow. Note that union , intersection , issubset , and difference all have operators that can be used as well.
16 Chapter 1. Introduction