Sie sind auf Seite 1von 10

‫‪Faculty of Computers & Informatics, Zagazig University‬‬

‫جامعة الزقازيق‬
‫كليه الحاسبات والمعلومات‬
‫الفرقة الثانية‬

‫المشوع البحث‬‫ر‬
‫المقدم عن مقرر‬
‫تصميم و تحليل الخواريزميات‬

‫محمد ناصر محمد عبدالرحمن‬ ‫اسم الطالب‬

‫‪2305‬‬ ‫رقم الجلوس‬

‫‪mn01838857@gmail.com‬‬ ‫االيميل‬
Faculty of Computers & Informatics, Zagazig University

Algorithms
Abstract
An algorithm is a procedure or formula for solving a problem, based on conducting a sequence of specified
actions. A computer program can be viewed as an elaborate algorithm. An algorithm typically Means a
simple process that solves a common problem in mathematics and in computer science. Algorithms are
commonly used in all IT (information technology) industries.

. For example, the search engine algorithm uses keyword and operator search strings as inputs, searches its
associated database for appropriate web pages and returns the results.The run time of the algorithm for a
particular entry depends on the number of operations performed. The more operations, the more time the
algorithm runs. Usually, we want to know how many operations the algorithm performs in comparison to the
size of its input, which we call N. Using the algorithm, the problem is divided into small pieces or steps and
therefore, it is easier for the programmer to convert it into an actual program.

Introduction
What is Algorithm?
The algorithm means "a process or set of rules that must be followed in calculations or other problem solving
operations ". Therefore, the algorithm refers to a set of rules / instructions that determine how to carry out the
work step-by-step to get the expected results. It can be understood by taking an example of cooking a new
recipe. To cook a new recipe, one reads instructions and steps and executes them one by one, in the specified
sequence. The result obtained is the perfectly cooked new dish. Likewise, algorithms assist in performing a
programming task to obtain the expected output.

Advantages of Algorithms:

1. It is a step-wise representation of a solution to a given problem, which makes it easy to understand.


2. An algorithm uses a definite procedure.
Faculty of Computers & Informatics, Zagazig University

3. It is not dependent on any programming language, so it is easy to understand for anyone even without
programming knowledge.
4. Every step in an algorithm has its own logical sequence so it is easy to debug.
5. By using algorithm, the problem is broken down into smaller pieces or steps hence, it is easier for
programmer to convert it into an actual program.
6. Effective communication: Algorithm is written using English like language, algorithm is better way of
communicating the logic to the concerned people.
7. Effective Analysis: with the help of algorithm, problems can be analyzed effectively, There can be
number of algorithms for solving given problem.
8. Proper Documentation: The selected algorithm has to be documented. The algorithm serves as a good
documentation which is required to identify the logical errors.
9. Easy and Efficient Coding: The algorithm act as blueprint during program development.
10. Program Debugging: Debugging: Debugging is nothing more than finding the algorithm / program logical
errors. Algorithms aid in debugging and we can quickly find the logical errors..
11. Program maintenance: Maintaining the software becomes much easier.

Shortest Path algorithm

definition

It is an algorithm to find the shortest paths between nodes in the graph, which may represent, for example,
road networks. The algorithm creates the shortest path tree from the start top, the source, to all other points
in the graph, and is called the Dijkstra algorithm.

The algorithm of Dijkstra finds the shortest path tree from a single source node by constructing a group of
nodes which are the lowest distance from the source.

• Advantages:-

1) It is used in Google Maps

2) It is used in finding Shortest Path.

3) It is used in geographical Maps

4) To find locations of Map which refers to vertices of graph.

5) Distance between the location refers to edges.


Faculty of Computers & Informatics, Zagazig University

6) It is used in IP routing to find Open shortest Path First.

7) It is used in the telephone network.

• Disadvantages:-

1) It do blind search so wastes lot of time while processing.

2) It cannot handle negative edges.

3) This leads to acyclic graphs and most often cannot obtain the right shortest path.

• Algorithm steps
1.Create a group (named sptSet) to track the headers in the shortest path tree, that is, the headers whose
minimum distance has been calculated from the source. This group is initially empty.

2.Assign a distance value to all vertices in the entered graph, then configure all distance values to be infinite
and assign a value of 0 to the source header so that it is the first vertex chosen by the algorithm.

3.Create an iterative loop provided that not all headers are in the sptSet group and this loop performs the
following steps:

3.1 Choose one of the u-vertices from the graph provided that it is not present in the sptSet group and has a
value for the minimum distance.

3.2 Add u to the sptSet group.

3.3 Update the distance value for all vertices adjacent to vertex u, and to do this we pass over all adjacent
vertices, and for each adjacent vertice v, if the set of the distance value of vertex u (from the source) and the
weight of the rib u-v is less than the vertex distance v, then we will update the distance of the v

The following diagram can be used to illustrate the steps of the Dijkstra algorithm:

. The sptSet group is initially empty, and the values for the header space in the graph are {0, INF, INF, INF,
INF, INF, INF, INF) (INF stands for infinitely). Now we choose the header that has the smallest distance
Faculty of Computers & Informatics, Zagazig University

value, the header 0, and add it to the sptSet group, The group becomes {0}.After adding header 0 to the
sptSet group, we update the distance values for the adjacent vertices, which are vertices 1 and 7, which will
update their distance value to 4 and 8. The following figure displays the vertices that have only specified
distance values, and the vertices in the shorter path tree SPT are colored In red color.

• The next step is to choose the header that has the lowest value for the distance provided that it is not
present in the shortest path tree (not in the sptSet group), so that the header 1 is chosen and added to the
sptSet group, and this group becomes {0, 1}, then We update the distance values for the heads adjacent to
head 1; Thus the head distance value of 2 is updated to 12.
• The next step is to choose the header that has the lowest value for
the distance provided that it is not present in the shortest path tree
(not found in the sptSet group), and then header 7 is chosen and
added to the sptSet group, thus this group becomes {0, 1, 7} , Then
update the distance values for the heads adjacent to head 7; Thus, the
distance value for Heads 6 and 8 is updated to 15 and 9, respectively.

The next step is to choose the header that has the lowest value for
the distance provided that it is not present in the shortest path tree
(not in the sptSet group), so that the header 6 is chosen and added to
the sptSet group, thus this group becomes {0, 1, 7, 6 }, Then update
the distance values for the headers next to head 6, Thus, the distance
value is updated for Heads 5 and 8.

• Repeat the previous steps until all conditions in the sptSet group are
met, at which point we get the shortest path tree.

Time complexity
Time Complexity of the implementation is O(V^2). If the input graph is represented using adjacency list, it
can be reduced to O(E log V) with the help of binary heap.

We have discussed Dijkstra’s algorithm and its implementation for adjacency matrix representation of
graphs. The time complexity for the matrix representation is O(V^2). In this post, O(ELogV) algorithm for
adjacency list representation is discussed.
Faculty of Computers & Informatics, Zagazig University

As discussed in the previous post, in Dijkstra’s algorithm, two sets are maintained, one set contains list of
vertices already included in SPT (Shortest Path Tree), other set contains vertices not yet included. With
adjacency list representation, all vertices of a graph can be traversed in O(V+E) time using BFS The idea is
to traverse all vertices of graph usingBFS and use a Min Heap to store the vertices not yet included in SPT
(or the vertices for which shortest distance is not finalized yet). Min Heap is used as a priority queue to get
the minimum distance vertex from set of not yet included vertices. Time complexity of operations like
extract-min and decrease-key value is O(LogV) for Min Heap.

Following are the detailed steps.


1) Create a Min Heap of size V where V is the number of vertices in the given graph. Every node of min
heap contains vertex number and distance value of the vertex.
2) Initialize Min Heap with source vertex as root (the distance value assigned to source vertex is 0). The
distance value assigned to all other vertices is INF (infinite).
3) While Min Heap is not empty, do following

a) Extract the vertex with minimum distance value node from Min Heap. Let the extracted vertex be u.
b) For every adjacent vertex v of u, check if v is in Min Heap. If v is in Min Heap and distance value is more
than weight of u-v plus distance value of u, then update the distance value of v.

Algorithm method

This algorithm follows the Greedy Algorithms method


Space complexity is the amount of memory used by the algorithm (including the input values to the
algorithm) to execute and produce the result.

Sometime Auxiliary Space is confused with Space Complexity. But Auxiliary Space is the extra space or the
temporary space used by the algorithm during it's execution.

Space Complexity = Auxiliary Space + Input space

Memory Usage while Execution

While executing, algorithm uses memory space for three reasons:

1. Instruction Space

It's the amount of memory used to save the compiled version of instructions.

Environmental Stack

Sometimes an algorithm(function) may be called inside another algorithm(function). In such a situation, the
current variables are pushed onto the system stack, where they wait for further execution and then the call to
the inside algorithm(function) is made.
Faculty of Computers & Informatics, Zagazig University

For example, If a function A() calls function B() inside it, then all th variables of the function A() will get
stored on the system stack temporarily, while the function B() is called and executed inside the funciton

A().Data Space

Amount of space used by the variables and constants.

But while calculating the Space Complexity of any algorithm, we usually consider only Data Space and we
neglect the Instruction Space and Environmental Stack.

=47 bytes

Code in c++
#include <stdio.h>

#include <limits.h>

#define V 9

int minDistance(int dist[], bool sptSet[])

int min = INT_MAX, min_index;

for (int v = 0; v < V; v++)

if (sptSet[v] == false && dist[v] <= min)

min = dist[v], min_index = v;

return min_index;

int printSolution(int dist[], int n)

printf("Vertex Distance from Source\n");

for (int i = 0; i < V; i++)

printf("%d tt %d\n", i, dist[i]);

void dijkstra(int graph[V][V], int src)

int dist[V];

bool sptSet[V];

for (int i = 0; i < V; i++)


Faculty of Computers & Informatics, Zagazig University

dist[i] = INT_MAX, sptSet[i] = false;

dist[src] = 0;

for (int count = 0; count < V-1; count++)

int u = minDistance(dist, sptSet);

sptSet[u] = true;

for (int v = 0; v < V; v++)

if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX

&& dist[u]+graph[u][v] < dist[v])

dist[v] = dist[u] + graph[u][v];

printSolution(dist, V);

int main()

int graph[V][V] = {{0, 4, 0, 0, 0, 0, 0, 8, 0},

{4, 0, 8, 0, 0, 0, 0, 11, 0},

{0, 8, 0, 7, 0, 4, 0, 0, 2},

{0, 0, 7, 0, 9, 14, 0, 0, 0},

{0, 0, 0, 9, 0, 10, 0, 0, 0},

{0, 0, 4, 14, 10, 0, 2, 0, 0},

{0, 0, 0, 0, 0, 2, 0, 1, 6},

{8, 11, 0, 0, 0, 0, 1, 0, 7},

{0, 0, 2, 0, 0, 0, 6, 7, 0}

};

dijkstra(graph, 0);

return 0;
Faculty of Computers & Informatics, Zagazig University
Faculty of Computers & Informatics, Zagazig University

References
‫تم االستعانة بموقع بنك المعرقة المصري وما ياتي بعض المراجع منه‬
,

• G. C. Clark, R. C. Davis, J. C. Herndon and D. D. McRae, Interim report on convolution coding research,
Sept. 1969.
• G. D. Forney, "Convolutional codes I: Algebraic structure", IEEE Trans. Inform. Theory, vol. IT-16, pp.
720-738, Nov. 1970.
• Hall, A. How to use Dijkstra's algorithm. Retrieved April 27, 2016,
• Hazzard, E. Tutorial. Retrieved April 24, 2016, from
• J. Viterbi, "Error bounds for convolutional codes and an asymptotically optimum decoding algorithm",
IEEE Trans. Inform. Theory, vol. IT-13, pp. 260-269, Apr. 1967.
• Skiena, S. Dijkstra's Algorithm Animation. Retrieved April 22, 2016,
• Hazzard,E.Tutorial.RetrievedApril24,2016,from

Das könnte Ihnen auch gefallen