Using an imperative definition, specify the abstract data type for a software stack

Một phần của tài liệu assignment name assignment 1 data structure and algorithm (Trang 28 - 38)

Speci Speci Speci Speci

Specify thfy thfy thfy thfy the e e e e abstrabstrabstrabstrabstraaaaactctctct data tyctdata tydata tydata tydata type for pe for pe for pe for a softpe for a softa softa softa software sware sware sware sware stttttaaaaack ck ck ck ck

A software stack is a set of programs that work together to produce a result, typically an operating system and its applications. For example, a smartphone software stack comprises the operating system along with the phone app, Web browser and other basic applications. A software stack may also refer to any group of applications that work in sequence toward a common result or any set of utilities or routines that work as a group. See stack, application stack and protocol stack. In addition, to allow them to work together, the required abstract data types such as Stack (LIFO) and Queue (FIFO).

So, in order to better understand the software stack, we find out about stack operation on Android device. For each application running on an Android device, the runtime system will maintain an active Stack. When an application is executed, the application's first operation is started to be placed on the stack. When a second operation is started, it is placed on the top of the stack and the previous activity is pushed down. Operation at the top of the recommended stack is active (or running). When the operation is exited, it is turned off the stack by the running time and the operation is located just below it in the stack to become the current activity. Operations at the top of the stack can, for example, just exit because the stack it is responsible for has been completed. In addition, the user may have selected the Back on screen button to return to the previous activity, causing the current activity to be disconnected from the stack by the runtime system and therefore destroyed. A visual representation of the Android Activity Stack is illustrated in Figure below:

As show in the diagram, new activities are pushed on to the top of the stack when they are started. The current active activity is located at the top of the stack until it is either pushed down the stack by a new activity, or popped off the stack when it exits or the user navigates to the previous activity. In the event that resources become constrained, the runtime will kill activities, starting with those at the bottom of the stack. The Activity Stack is what is referred to in programming terminology as a Last- In- First- Out (LIFO) stack in the last item to be pushed onto the stack is the first to be popped off.

VI. T VI. T VI. T VI. T

VI. Two netwo netwo netwo netwo network work work work work shshshshshortest ortest ortest ortest ortest papapapapattttth algh algh algh algh algorororororithithithithmithmmmmsssss 1. B

1. B 1. B 1. B

1. Bellman-ellman-ellman-ellman-Ford Algellman-Ford AlgFord AlgFord AlgorithFord Algorithorithorithorithms ms ms ms ms

Bellman-Ford algorithm solves the problem of single source in the general case, in which edges can have negative weights and oriented graphs. If the graph is not guided, it will have to be modified by including two edges in each direction to make it oriented. Bellman-Ford has assets that can detect negative weight cycles accessible from the source, which means that no shortest path exists. If there is a negative weight cycle, a path can run indefinitely on that cycle, reducing the path cost down. Without a negative weight cycle, Bellman-Ford returned the weight of the shortest road along the same road.

How the Bellman-Ford Algorithms work?

The Bellman Ford algorithm works by overestimating the length of the path from the starting peak to all other vertices. After that, it repeats those estimates by finding new paths shorter than previously overestimated paths. Like other dynamic programming problems, the algorithm calculates the shortest path in a bottom-up way. First, it calculates the shortest distance that has at most one edge in the path. Then it calculates the shortest path with up to 2 edges, etc.

After the iteration of the external loop, the shortest path with at most i edges is calculated.

There may be a maximum | V | - 1 edge in any simple path, that's why the outer loop runs | v | - 1 times. The idea is, assuming that there is no negative weight cycle, if we have calculated the shortest paths with most edges i, then repeating on all edges ensures the shortest path to be given the most edges (i + 1). Below are the detailed steps used in Dijkstra’s algorithm to find the shortest path from a single source vertex to all other vertices in the given graph. Given a graph and a source src peak in the graph, find the shortest path from src to all vertices in the given graph. The chart may contain negative weight edges.

Algorithm

Create a set sptSet (shortest path tree set) that keeps track of vertices included in shortest path tree, i.e., whose minimum distance from source is calculated and finalized. Initially, this set is empty.

Assign a distance value to all vertices in the input graph. Initialize all distance values as INFINITE. Assign distance value as 0 for the source vertex so that it is picked first.

While sptSet doesn’t include all vertices

Pick a vertex u which is not there in sptSet and has minimum distance value.

Include u to sptSet.

Update distance value of all adjacent vertices of u. To update the distance values, iterate through all adjacent vertices. For every adjacent vertex v, if sum of distance value of u (from source) and weight of edge u-v, is less than the distance value of v, then update the distance value of v. To better understand how it works as well as the algorithm of the Bellman Ford Algorithm, we will provide you with an example. Set the source vertex given to 0. Initialize all distances as infinite, except the distance to the source itself. The total number of vertices in the graph is 5, so all edges must be processed 4 times.

Let all edges be processed in the following order: (B, E), (D, B), (B, D), (A, B), (A, C), (D, C), ( B, C), (E, D). We get distance after all edges are processed for the first time. The first row in the

original distance. The second row shows the distance when the edges (B, E), (D, B), (B, D) and (A, B) are processed. The third row displays the distance when (A, C) is processed. The fourth row displayed when (D, C), (B, C) and (E, D) are processed.

The second iteration ensures that all the shortest paths are at most 2 edges long. The algorithm handles all edges 2 more times. The distance is minimized after the second iteration, so the third iteration and the fourth time do not update the distance.

2. D 2. D 2. D 2. D

2. Dijkstrijkstrijkstrijkstrijkstra’s Aa’s Aa’s Aa’s Aa’s Algoritlgoritlgoritlgoritlgorithm hm hm hm hm

Dijkstra's algorithm uses the first width search (not the single-source shortest path algorithm) to solve a single source problem. It places a constraint on the graph: it cannot have negative weight edges. However, with one limitation, Dijkstra significantly improved the running time of Bellman. Dijkstra's algorithm is sometimes also used to solve the shortest path problem of all pairs by simply running it on all internal vertices. Again, this requires all edge weights to be positive. Dijkstra's algorithm allows you to calculate the shortest path between a node (you choose which button) and every other node in the graph.

How Dijkstra's Algorithm works

Djkstra's algorithm works on the basis that all B -> D paths of the shortest path A -> D between vertices A and D are also the shortest path between vertices B and D.

Djikstra used this property in the opposite direction, that is, we overestimate the distance of each vertex from the start. Then we visit each of its nodes and neighbors to find the shortest path to those neighboring nodes. The algorithm uses a greedy approach in the sense that we find the next best solution in the hope that the end result is the best solution for the whole problem. For instances, calculate the shortest path between node C and the other nodes in the chart below:

During the execution of the algorithm, we will mark every node with the minimum distance to the C button (our selected node). For node C, this distance is 0. For the rest of the nodes, since we still don't know the minimum distance, it starts with infinity (∞):

We will also have an existing node. Initially, we set it to C (our selected button). In the picture, we mark the current button with a red dot. Now, we check the neighbors of our current node (A, B and D) in no particular order. Let's start with B. We add the minimum distance of the current node (in this case 0) with the weight of our current connection node with B (in this case 7) and them I have 0 + 7 = 7. We compare that value to the minimum distance of B (infinity);

the lowest value is the remaining value is the minimum distance of B (in this case, 7 is smaller

As far as possible. Now, check neighbors A. We add 0 (the minimum distance of C, our current node) with 1 (the weight of our current connected node with A) to get 1 We compare 1 with the minimum distance of A (infinity) and leave the smallest value:

Repeat the same process for D:

Then, we checked all neighbors of C. Because of that, we marked it as accessed. Please indicate the buttons accessed with a green check mark:

Now we need to select a new existing node. The button must be an unwanted node with the smallest minimum distance (so the node has the smallest number and no check marks). That is A. Please mark it with a red dot:

And now we repeat the algorithm. We check our current node's neighbor, ignoring the accessed buttons. This means we only test B. For B, we add 1 (minimum distance of A, our current node) to 3 (weights of edges A and B) to get 4. We compare 4 with minimum distance of B (7) and leave the smallest value: 4.

Then we marked A as visited and selected a new existing node. D, which is the non-access node

We repeat the algorithm again. This time, we checked B and E. For B, we get 2 + 5 = 7. We compare that value to the minimum distance of B (4) and leave the minimum value (4). For E, we obtain 2 + 7 = 9, compare it with the minimum distance of E (infinity) and leave the smallest (9). We marked D as accessed and put our current button into B.

So, we just need to check E. 4 + 1 = 5, smaller than the minimum distance of E (9), so we leave 5. After that, we marked B as accessed and set. E is the current node.

Since no buttons are not provided, we are done! The minimum distance of each node now actually represents the minimum distance from that node to the C button (the button we chose as the original button)

3. Co 3. Co 3. Co 3. Co

3. Comparismparismparismparismparisooooon n n n n BBBBBeeeeellllllllllman man man man man Ford’s Ford’s Ford’s Ford’s AlgoritFord’s AlgoritAlgoritAlgoritAlgorithm hm hm hm hm and Dand Dand Dand Dand Dijijijijijktra’sktra’sktra’sktra’sktra’s Algor Algor Algor Algor Algorithithithithmithmmmm

BELLMAN FORD’S ALGORITHM DIJKSTRA’S ALGORITHM

Bellman Ford’s Algorithm works when there is negative weight edge, it also detects the negative weight cycle.

Dijkstra’s Algorithm doesn’t work when there is negative weight edge.

The result contains the vertices which contains the information about the other vertices they are connected to.

The result contains the vertices containing whole information about the network, not only the vertices they are connected to.

It can easily be implemented in a distributed way.

It cannot be implemented easily in a distributed way.

It is relatively less time consuming.

It is more time consuming than Bellman Ford’s algorithm.

Dynamic Programming approach is taken to implement the algorithm.

Greedy approach is taken to implement the algorithm.

Một phần của tài liệu assignment name assignment 1 data structure and algorithm (Trang 28 - 38)

Tải bản đầy đủ (PDF)

(38 trang)