Decision Flashcards
Algorithm - definition
> An algorithm is a finite sequence of step-by-step instructions carried out to solve a problem.
Flow chart
> In a flow chart, the shape of each box tells you its function.
How can unordered lists be sorted?
- Bubble sort
- Quick sort
>Quick sort is quicker to implement than bubble sort except when e.g. only the largest item is out of place when putting them in ascending order.
Bubble sort
> In a bubble sort, you compare adjacent items in a list:
-if they are in order, leave them.
-if they are not in order, swap them.
-the list is in order when a pass is completed without any swaps.
0.5n(n-1) = (n-1) + (n-2) + … + 2 + 1.
Min passes = 1 (already in order)/
Max passes = n (if smallest item is at the end on the list).
Quick sort
> In a quick sort, you select a pivot then split the items into two sub-lists:
-one sub-list contains items less than the pivot.
-the other sub-list contains items greater than the pivot.
-you then select further pivots within each sub-list and repeat the process.
nlogn
The 3 bin-packing algorithms
- First-fit algorithm
- First-fit decreasing algorithm
- Full-bin packing algorithm
First-fit algorithm
> The first-fit algorithm works by considering items in the order they are given.
Quick to implement.
Not likely to lead to a good solution.
First-fit decreasing
> The first-fit decreasing algorithm requires the items to be in descending order before applying the algorithm.
Usually a good solution and easy to implement.
Not likely to lead to a good solution.
Full-bin packing
> Full-bin packing uses inspection to select items that will combine to full bins. Remaining items are packed using the first-fit algorithm.
Usually a good solution.
Difficult to do, especially when the numbers are plentiful or awkward.
Order of an algorithm
> The order of an algorithm can be described as a function of its size.
If an algorithm has order f(n), then increasing the size of the problem from n to m will increase the run time of the algorithm by a factor of approximately f(m) / f(n).
If the size of a problem is increased by some factor k then an algorithm of linear order will take approx. k times as long to complete, quadratic - k^2 etc.
Graph - description
> A graph consists of points (called vertices or nodes) which are connected by lines (edges or arcs).
Weighted graph/network
> If a graph has a number associated with each edge (usually called its weight), then the graph is known as a weighted graph or network.
Subgraph - definition
> A subgraph is a graph, each of whose vertices belongs to the original graph and each of whose edges belongs to the original graph.
It is part of the original graph.
Degree/valency/order of a vertex - definition
> The degree (or valency or order) of a vertex is the number of edges incident to it.
If the degree of a vertex is even, you say that it has even degree.
If the degree of a vertex is odd it has odd degree.
Walk - definition
> A walk is a route through a graph along edges from one vertex to the next.
Path - definition
> A path is a walk in which no vertex is visited more than once.
Trail - definition
> A trail is a walk in which no edge is visited more than once.
Cycle - definition
> A cycle is a walk in which the end vertex is the same as the start vertex and no other vertex is visited more than once.
Hamiltonian cycle - definition
> A Hamiltonian cycle is a cycle that includes every vertex.
Connected
> 2 vertices are connected if there is a path between them.
>A graph is connected if all its vertices are connected.
Loop - definition
> A loop is an edge that starts and finishes at the same vertex.
Simple graph - definition
> A simple graph is one in which there are no loops and there is at most one edge connecting any pair of vertices.
Directed graph - definition
> If the edges of a graph have a direction associated with them they are known as directed edges and the graph is known as a directed graph (or digraph).
Euler’s handshaking lemma
> In any undirected graph, the sum of the degrees of the vertices is equal to 2 x the number of edges.
As a consequence, the number of odd nodes must be even.
This result is called Euler’s handshaking lemma.
Any vertices of odd degree must therefore ‘pair up’.
Tree - definition
> A tree is a connected graph with no cycles.
Spanning tree - definition
> A spanning tree of a graph is a subgraph which includes all the vertices of the original graph and is also a tree.
Complete graph - definition
> A complete graph is a graph in which every vertex is directly connected by a single edge to each of the other vertices.
Isomorphic graph - definition
> Isomorphic graphs are graphs which show the same information but may be drawn differently.
Adjacency matrix
> Each entry in an adjacency matrix describes the number of arcs joining the corresponding vertices.
Distance matrix
> In a distance matrix, the entries represent the weight of each arc, not the number of arcs.
Planar graph - definition
> A planar graph is one that can be drawn in a plane such that no two edges meet except at a vertex.
Planarity algorithm
> The planarity algorithm may be applied to any graph that contains a Hamiltonian cycle.
It provides a method of redrawing the graph in such a way that it becomes clear whether or not it is planar.
Kn
> Complete graph with n vertices.
Q. Total no. off edges of K20
> 190
0.5n(n-1)
0.5 x 20 x 19 = 190
Q. Edges in spanning tree with v vertices
> v-1
Q. Why can’t there be a graph with 4 vertices of degrees 3, 1, 1, 2?
> 3+1+1+2 = 7 = odd.
>Sum of the degrees of the vertices must be even.
Minimum Spanning Tree - definition
> A minimum spanning tree (MST) is a spanning tree such that the total length of its arcs is as small as possible.
Kruskal’s Algorithm
> Kruskal’s algorithm can be used to find a MST.
Sort the arcs into ascending order of weight and use the arc with the least weight to start the tree.
Then add arcs in order of ascending weight, unless an arc would form a cycle, in which case reject it.
Prim’s algorithm
> Prim’s algorithm can be used to find a MST.
Choose any vertex to start the tree.
Then select an arc of least weight that joins a vertex already in the tree to a vertex not yet in the tree.
Repeat this until all vertices are connected.
Prim’s algorithm and distance matrices
> Prim’s algorithm can be applied to a distance matrix.
Choose any vertex to start the tree.
Delete the row in the matrix for the chosen vertex and number the column in the matrix for the chosen vertex.
Ring the lowest undeleted entry in the numbered columns, which becomes the next arc.
Repeat this until all rows are deleted.
Dijkstra’s algorithm
> Dijkstra’s algorithm can be used to find the shortest path between two vertices in a network:
-label the start vertex with final value 0.
-record a working value at every vertex, Y, that is directly connected to the vertex that has just received its final label, X: final label at X + plus weight of arc XY.
-select the smallest working value. This is the final label for that vertex.
-repeat until the destination vertex receives its final label.
-find the shortest path by tracing back from destination to start. Include an arc AB on the route if B is already on the route and final label B - final label A = weight of arc AB.
Each vertex is replaced by a box.
What does Dijkstra’s algorithm do and when is it used?
> Dijkstra’s algorithm finds the shortest route between the start vertex and each intermediate vertex completed on the way to the destination vertex.
It is possible to use Dijkstra’s algorithm on networks with directed arcs, such as a route with one-way streets.
Floyd’s algorithm
> Floyd’s algorithm can be used to find the shortest path between every pair of vertices in a network:
Floyd’s algorithm applied to a network with n vertices produces two tables as a result of n iterations.
One table shows the shortest distances and the other contains information about the corresponding paths taken.
Q. Why don’t you need to check for cycles when using Prim’s?
> Prim’s algorithm identifies the next node to link to the existing tree. Linking a new node can’t form a cycle.
Q. Why isn’t this MST unique?
> E.g. AB can be replaced with AT as they both have the same weight of 10.
Q. Explain the differences between Prim’s and Kruskal’s.
- In Prim’s the starting point can be any node, but Kruskal’s starts from the arc of least weight.
- In Prim’s, each new node is added to the existing tree as it builds, whereas in K, the arcs are selected according to their weight and may not be connected until the end.
Q. Workout the no. of comparisons required for an n x n distance matrix (Prim’s) and state the order in terms of number of vertices, n.
> Starting with an n x n matrix, the row corresponding to the starting vertex is crossed out, leaving n - 1 values in the corresponding column.
The smallest of these is to be found. This requires (n - 1) - 1 comparisons.
At each stage, the number of columns included increases by 1 and the number of values remaining in each of those columns reduces by 1.
The total number of comparisons required to complete the algorithm is given by:
((n - 1) x 1 - 1) + ((n-2) x 2 - 1) + ((n-3) x 3 -1) +…+ ((n-(n-1)) x (n - 1) - 1).
Which is sigma (n-1 on the top and r=1 on the bottom) followed by ((n-r) x r -1) = nr - r^2 - 1.
n(0.5(n-1)n) - 1/6(n-1)n(2n-1) - (n-1).
(n^3 - 7n + 6)/6
Order = n^3.
Q. Explain the difference between the output of Dijkstra’s algorithm and Floyd’s algorithm
> The output of Floyd’s algorithm gives the shortest distance between every pair of nodes.
The output of Dijkstra’s algorithm gives the shortest distance from the start node to every other.
Tables used in Floyd’s
- Route table
2. Distance table
Q. Floyd’s algorithm is applied to an n x n distance matrix. State the number of comparisons that are made with each iteration and give the order of Floyd’s algorithm.
> (n-1)(n-2) = n^2 -3n +2.
>Cubic.