D1 Notes

11
D1 Notes Chapter 1 - Algorithms Algorithm – Precise set of instructions – very clear so that anyone/computer can follow it. Flow Chart - type of algorithm. They can be used to design computer programs. Bubble Sort Algorithm – used to sort an unsorted list into alphabetical / numerical order. We compare adjacent items. 1. Start at the beginning of the list and past through, comparing adjacent values. If they are in order, leave them. If they are not in order, swap them. 2. At the end of the list, repeat step 1. 3. When you pass through without any swaps, the list is in order. Quick Sort Algorithm – Quick and efficient. Can put into ascending or descending order. 1. Choose the item at the mid-point of the list to be the first pivot. 2. Write down all the items that are less than the pivot, keeping their order, in a sub-list. 3. Write down the pivot. 4. Write down the remaining items (greater than the pivot) in a sublist. 5. Apply steps 1 to 4 to each sub-list (almost like zooming in on each sublist). 6. When all items have been chosen as pivots, stop. Binary Search Algorithm – searches an ordered list to see if a specific item is contained in the list (you may need to use another algorithm first to order the list). To search an ordered list of n items for a target T: Start/ End Instructio n Decision

description

Notes for D1 exam.

Transcript of D1 Notes

D1 Notes

Chapter 1 - Algorithms

Algorithm Precise set of instructions very clear so that anyone/computer can follow it.

Flow Chart - type of algorithm. They can be used to design computer programs.

DecisionInstructionStart/End

Bubble Sort Algorithm used to sort an unsorted list into alphabetical / numerical order. We compare adjacent items. 1. Start at the beginning of the list and past through, comparing adjacent values. If they are in order, leave them. If they are not in order, swap them. 2. At the end of the list, repeat step 1. 3. When you pass through without any swaps, the list is in order.

Quick Sort Algorithm Quick and efficient. Can put into ascending or descending order. 1. Choose the item at the mid-point of the list to be the first pivot. 2. Write down all the items that are less than the pivot, keeping their order, in a sub-list.3. Write down the pivot.4. Write down the remaining items (greater than the pivot) in a sublist.5. Apply steps 1 to 4 to each sub-list (almost like zooming in on each sublist).6. When all items have been chosen as pivots, stop.

Binary Search Algorithm searches an ordered list to see if a specific item is contained in the list (you may need to use another algorithm first to order the list).

To search an ordered list of n items for a target T:1. If T = mstop the target has been found.2. T before m cannot be in second half therefore that half and m are discarded.3. T after mcannot be in first half therefore that half and m are discarded.4. Repeat steps 1 to 3 until it has been located.

Bin Packing Algorithms bin packing refers to lots of problems i.e. stacking boxes of a certain length a and width b but varying height into bins with the same length a and width b. Other ideas are cars onto a ferry with lanes of equal length how do we fit the most cars in? We use three bin packing algorithms:

First-fit: 1. Take the items in the order given.2. Place each item in the first available bin that can hold it. Start from bin 1 each time. Advantage : QuickDisadvantage: Rarely leads to good or optimal solution.

First-fit decreasing:1. Reorder the items so that they are in descending order.2. Apply the first-fit algorithm to the reordered list. Advantage: Usually get a fairly good solution and easy to do.Disadvantage: May not get an optimal solution.

Full-bin packing1. Use observation to find combinations that make up a full bin. 2. Any remaining items are packed using the first-fit algorithm.Advantage: Usually get a good solution. Disadvantage: Difficult to do, especially when numbers are plentiful and awkward.

Chapter 2 Graphs and Networks

Graph - consists of points (vertices/nodes) which are connected by lines (edges or arcs).A graph may have a weight attached to it (i.e. a number on each edge). These graphs are called weighted graphs/networks. Vertices are called A, B, C etc. Edges are called AB, BC etc. A subgraph of A is a graph where all of its vertices and edges also belong to A. It is a part of the graph. The degree/valency/order of a vertex is the number of edges incident to it. A path is a finite sequence of edges through a graph where no vertex appears more than once. A walk is a path in which you can visit a vertex more than once. A cycle is a closed path i.e. end vertex of last edge is start vertex of first edge. A graph is connected if all its vertices are connected. A loop is an edge that starts and finishes at the same vertex. A simple graph has no loops and no more than one edge connecting two vertices. A digraph has directed edges i.e. a direction associated with the edges.

Special types of Graph: Tree connected graph with no cycles.

Spanning Tree of G includes all vertices of G and is a tree.

Bipartite graph Two sets of vertices, X and Y, where no edge joins X to X, only X to Y.

Complete graph Every vertex is directly connected by an edge to each of the other vertices.

Complete bipartite graph- Every value in set X joins every value in set Y.

Isomorphic graph Graphs that show the same information but are drawn differently.

Adjacency matrix records that number of direct links between vertices. Distance matrix records the weights on the edges. Where there is no edge, write

Chapter 3 - Algorithms on Networks

A minimum spanning tree is a spanning tree where the total length of the arcs is as small as possible.

Kruskals algorithm finds the shortest, cheapest or fastest way of linking all nodes into one system. The algorithm: 1. Sort all the arcs into ascending order of weight. 2. Select the arc of least weight to start the tree.3. Consider the next arc of least weight. If it would form a cycle then reject it. If it does not form a cycle, add it to the tree.4. Repeat step 3 until all vertices included.

Prims Algorithm uses a different approach. 1. Choose any vertex to start the tree.2. Select an arc of least weight that joins a vertex that is already in the tree to a vertex that isnt. Choose randomly if there is a choice of arcs of equal weight. 3. Repeat step 2 until all vertices connected.

Applying Prims Algorithm to a distance matrix: Networks can be described in distance matrix form. They can be input into computers in this form. Prims algorithm is easily adapted into distance matrix so is more easily computerised. Distance matrix form:1. Choose any vertex to start the tree.2. Delete the row in the matrix for the chosen vertex.3. Number the column in the matrix for the chosen vertex. 4. Put a ring around the lowest undeleted entry in the numbered columns. 5. Ringed entry becomes the next arc to be added to the tree. 6. Repeat steps 2,3,4 and 5 until all rows are deleted.

Dijkstras algorithm is used to find the shortest, cheapest or quickest route between two vertices. i.e. shortest car journey from school to the sports centre. 1. Label the start vertex, S, with the final label 0. 2. Record a working value at EVERY vertex, Y, that is directly connected to vertex X that has just received its final label. Working value at Y = final value at X + weight of arc XY. If there is already a working value at Y, it is only replaced if the new value is smaller. Once a vertex has a final label it is not revisited and its working values are no longer used. 3. Look at the working values at all vertices without final labels. Select the smallest working value. This now becomes the final label at that vertex. 4. Repeat steps 2 and 3 until the destination vertex, T, receives its final label. 5. To find the shortest path, trace back from T to S. Given that B already lies on the route, include arc AB whenever final label of B final label of A = weight of arc AB. Dijkstras algorithm finds the shortest route between the start vertex and each intermediate vertex completed on the way to the destination vertex.

You can used Dijkstrass algorithm on networks with directed arcs, similar to finding a route when some of the roads are one way streets.

Chapter 4 Route Inspection (Chinese Postman Problem)

Traversable graphs - where you can travel along every arc just once without taking your pen from the paper. Traversable if all valencies are even. Semi-traversable is it has precisely two odd valencies. Not traversable if more than two odd vertices.

Eulerian graphs are when all valencies in a graph are even. Semi-eulerian if two valencies are odd and the rest are even.

Route inspection problem finds the shortest route that traverses every arc at least once and returns to the starting point. If all vertices have an even valency then the network is traversable. The length of the shortest route is equal to the weight of the network.

1. Identify any vertices with odd valency.2. Consider all possible complete pairings of these vertices.3. Select the complete pairing that has the least sum.4. Add a repeat of the arcs indicated by this pairing to the network.

Chapter 5 Critical Path Analysis

Critical path analysis concerns organizing projects effectively, so that you can have the minimum workers and compete projects in the minimum amount of time.

Some activities cannot be started until others finish.

We use precedence tables to show which jobs must be finished before others completed. E.g. if looking at the project of building a house, we cannot have the electrician coming in before the foundations are laid!

We use activity networks to represent our precedent tables in a clear manner; they are easier to understand if the problem is complex. We use the activity on arc type network here. The activities are represented by arcs and the completion of those activities (events) are shown as nodes. Each arc is labeled with an activity letter.The beginning and end of an activity are shown at the ends of the arc and an arrow is used to define the direction. We use straight lines for arcs. Nodes are numbered starting from 0 for the first node source node. Number each node as it is added to the network. Final node sink node.

We use dummies in activity networks for precedence tables. These are used when we cannot complete the activity network and show that one activity depends on two others for example. Dummies have no time or cost.

There can be at most one activity between any two events. A dummy may be used here to help satisfy this condition.

Carrying out a forward pass using early and late event times. Duration of the activity How long each activity takes to complete. Early event time earliest time of arrival allowing for the activities beforehand to be completed.Late event time latest time that event can be left without extending the time of the whole thing. Early event times are calculated starting from 0 at the source node and working towards the sink node. This is a forward pass or forward scan. Late event times are calculated starting from the sink node and working backwards towards the source node. This is a backward pass or backward scan.

Critical activities an activity where any increase in its duration results in an overall increase in the project completion time. A path from the source node to the sink node, which follows entirely critical activities, is called the critical path. On a critical path, each node has an early event time equal to the late event time.An activity connecting two critical events is not necessarily critical.

Total float of an activity is the amount of time that its start time may be delayed by without affecting the duration of the project. Total float = latest finish time duration earliest start timeThe total float of a critical activity is 0.

We can use cascade (Gannt) charts to show possible start and finish times of activities in a graphical way. The number scale used shows elapsed time i.e. the first hour is between 0 and 1, the second hour is between 1 and 2 and so on (very simple way to catch you out!) The critical activities always lie along the top the chart shows there is no flexibility in the timing of the critical activities but that there is a degree of flexibility for non-critical activities. The total float of each activity is represented by the range of movement of its rectangle on the chart (shown as a dotted box).

We can construct scheduling diagrams to show how workers can be used to ensure the project is completed in the minimum time possible. Each activity is completed by a single worker in the time given as the duration of the activity. Once an activity has started, it must be completed by that worker. Once a worker has completed an activity he/she becomes immediately available to start another activity. Always use the first available worker.

The lower bound for the number of workers needed to complete a project within its critical times is given by the smallest integer greater than or equal to:

Sometimes it is better to construct the scheduling diagram straight from the activity network, rather than the Gannt chart, because it may be necessary to delay some activities further if the amount of workers available is less than the lower bound needed.

Chapter 6 Linear Programming

Formulating a problem as a linear programming problem. Decision variables are the number of each of the things that can be varied i.e. number of cakes made etc. The variables are represented by letters; they become the letters of the inequalities and the objective function. Objective function the aim of the problem i.e. to maximize profit or minimize cost. There are 2 parts: 1) maximize or minimize and 2) an algebraic expression written as an equation in terms of the decision variables i.e. maximize P subject to the constraints P = 3x + 2y Constraints things that will prevent you making/using an infinite number of each of the variables. i.e. quantity of raw materials available, time available, cant be negative etc. Each constraint makes 1 inequality. Feasible solution when final values for decision variables satisfy the constraints. Feasible region region that contains ALL feasible solutions. Optimal solution feasible solution that meets the objective. There may be more than 1.

To formulate a problem as a linear programming problem:1) Define the decision variables (x, y, z etc)2) State the objective (to maximize or minimize with an algebraic expression)3) Write the constraints as inequalities.

Two variable linear programming problems can be represented graphically. Once a straight line is drawn onto the graph, there are 3 regions: the line, above the line and below the line. All points on the line are represented by an equals, above > and below use a dotted line to show that the line itself is not included in the region. Inequalities using are represented by a solid line to show that the line is included in the region. We shade the part of the reason that we DO NOT WANT i.e. we are getting rid of it. The feasible region satisfying the inequalities will be left UNSHADED.

We use the ruler method to locate the optimal point in a feasible region. There will be many potential solutions left in the unshaded region so we need to find the optimal one. We draw on the objective line on to the graph and then slide a ruler so that it is parallel to the objective line. Maximum point look for the last point covered by an objective line as it leaves the feasible region. Minimum point look for the first point covered by an objective line as it enters the feasible region. A set square is a useful tool in helping to keep the ruler parallel to the objective line. We can also use the vertex testing method to locate the optimal point. An optimal point occurs at one or more of the vertices of the feasible region.1) First find the coordinates of each vertex of the feasible point. 2) Evaluate the objective function at each point.3) Select the vertex that gives the optimal value of the objective function.

There may be an addition constraint- solutions needing integer values. You may have to therefore find the optimal integer solution by drawing dots on all the integer solutions and then finding the last/first integer solution covered by the objective line as it slides across the feasible region. The above may not always be accurate, so you can also find the optimal non-integer solution and then test the four vertices around it (if they lie in the feasible region) to see which gives the optimal solution.

Chapter 7 Matchings

You may need to show matchings using a bipartite graph A bipartite graph has two sets of nodes.

Arcs may connect node from different sets but never connect nodes in the same set. We may need to form matchings; a 1 to 1 pairing of some or all of the elements in one set, X, with elements of a second set, Y i.e. only 1 node in X is paired to 1 node in Y. A matching pairs off the nodes. If both sets have n nodes, a complete matching is a matching with n arcs. A maximal matching may need to be found if a complete matching is not possible.

Maximum Matching Algorithm When you have drawn a bipartite graph, you find an intital matching then apply the maximum matching algorithm to improve the matching. (in the exam you will be given the initial matching with which to start). Alternating path starts at unmatched node on one side of the graph and finishes at an unmatched node on the other side. It uses arcs that are alternately in or not in the initial matching. This path can only match people to tasks they are willing to do.

1) Start with any initial matching. 2) Search for an alternating path. 3) If an alternating path is found, use it to create an improved matching by changing the status of the arcs. If one cannot be found, stop. 4) List the new matching which is the alternating path found and any other unchanged elements from the initial matching. 5) If the matching is now complete, stop. If not, go back to step 2. If you find an alternating path, a breakthrough has occurred. You need to change the status (i.e. switch not in to in).