Visvesvaraya Technological University: "Dijkstra'S Algorithm"
Visvesvaraya Technological University: "Dijkstra'S Algorithm"
Visvesvaraya Technological University: "Dijkstra'S Algorithm"
BELGAVI-590018
A CG Mini-Project Report
On
“DIJKSTRA’S ALGORITHM”
A Mini project report submitted in partial fulfilment of the requirements for the 6th
semester of Bachelor of Engineering in Computer Science and Engineering
of Visvesvaraya Technological University, Belagavi
Submitted by:
RASHMI .C 1RN19CS410
CERTIFICATE
Certified that the mini project work entitled “DIJKSTRA’S ALGORITHM” has been
successfully carried out by RASHMI .C bearing USN 1RN19CS410 and SINCHANA
bearing USN 1RN18CS103, bonafide students of RNS Institute of Technology in partial
fulfillment of the requirements for the 6th semester of Bachelor of Engineering in
Computer Science and Engineering of Visvesvaraya Technological University, Belgaum,
during academic year 2020-2021. It is certified that all corrections/suggestions indicated for
Internal Assessment have been incorporated in the report deposited in the departmental
library. The project report has been approved as it satisfies the CG laboratory requirements of
6th semester BE, CSE.
External Viva:
1.
2.
ACKNOWLEDGMENT
We would like to thank Dr. H N Shivashankar, Director, RNSIT, Bangalore, for his
moral support towards completing our project.
We would like to thank Dr. Kiran P, Prof. and Head Department of Computer
Science & Engineering, RNSIT, Bangalore, for his valuable suggestions and expert advice.
We would like to thank all the teaching and non-teaching staff of department of
Computer Science & Engineering, RNSIT, Bengaluru for their constant support and
encouragement.
Date:10/08/2021 RASHMI .C
Place: Bengaluru SINCHANA
ABSTRACT
1. Introduction 1
1.1Introduction to Computer Graphics 1
1.2Introduction to OpenGL 3
2. Requirements specification 7
2.1Software requirements 7
2.2Hardware requirements 7
3. System definition 8
3.1Algorithm 8
3.2Flow diagram 10
3.3Functions used 11
4. Implementation 12
4.1Source code 12
5. Testing and Results 22
5.1Different types of testing 22
5.2Snapshots 23
6. Conclusion 26
7. Future enhancement 27
8. Bibliography 28
Dijkstra’s Algorithm
CHAPTER 1
INTRODUCTION
Mouse: A computer mouse is a hand held pointing device that detects two-
dimensional motion relative to the surface. This motion is typically translated into
motion of a pointer on a display, which allows a smooth control of the graphical user
interface.
Processor: A graphics processing unit is able to render images more quickly than a
central processing unit because of its parallel processing architecture, which allows it
to perform multiple calculations at the same time.
Memory: The term memory identifies data storage that comes in the form of chips,
and the word storage is used for memory that exists on tapes or disks. Every computer
comes with a certain amount of physical memory referred as main memory.
Frame Buffer: A frame buffer is a portion of RAM containing a bitmap that drives a
video display. It is a memory buffer containing a complete frame of data. The
information in buffer typically consists of color values for every pixel to be shown on
the display.
Output Device: Graphics hardware output devices are those hardware that generates
computer graphics and allow them to be shown on a display.
APIs are nevertheless powerful. It supports the simple 2D and 3D programs. It also supports
the advanced rendering techniques. OpenGL API explains following 3 components
1. Graphics functions
2. Graphics pipeline and state machines
3. The OpenGL interfaces
There are so many polygon types in OpenGL like triangles, quadrilaterals, strips and fans.
There are 2 control functions, which will explain OpenGL through,
1. Interaction with window system
2. Aspect ratio and view ports
GLUT is the OpenGL Utility Toolkit, a window system independent toolkit for
writing OpenGL programs. It implements a simple windowing application
programming interface (API) for OpenGL. GLUT makes it considerably easier to
learn about and explore OpenGL programming. GLUT provides a portable API, so
you can write a single OpenGL program that works across all PC and workstation OS
platforms.
GL (Graphics Library): Library of 2-D,3-D drawing primitives and operations.
GLX ("OpenGL Extension to the X Window System") is an extension to the X
Window System core protocol providing an interface between OpenGL and the X
Window System as well as extensions to OpenGL itself. It enables programs wishing
to use OpenGL to do so within a window provided by the X Window System.
Xlib is a library which serves as an interface between a programming language and
the hardware of the graphics card and monitor. It abstracts details about the graphics
card and monitor hardware.
Xtk is a WebGL framework providing an easy-to-use API to visualize scientific data
on the web.
A framebuffer (frame buffer, or sometimes frame store) is a portion of RAM
containing a bitmap that drives a video display. It is a memory buffer containing a
complete frame of data. Modern video cards contain framebuffer circuitry in their
cores. This circuitry converts an in-memory bitmap into a video signal that can be
displayed on a computer monitor[6].
Display Lists: All data, whether it describes geometry or pixels, can be saved in a
display list for current or later use. When a display list is executed, the retained data is
sent from the display list just as if it were sent by the application in immediate mode.
Evaluators: All geometric primitives are eventually described by vertices. Parametric
curves and surfaces may be initially described by control points and polynomial
functions called basis functions. Evaluators provide a method to derive the vertices
used to represent the surface from the control points. The method is a polynomial
mapping, which can produce surface normal, texture coordinates, colors, and spatial
coordinate values from the control points[1].
Per-Vertex Operations: For vertex data, next is the "per-vertex operations" stage,
which converts the vertices into primitives. Some vertex data (for example, spatial
coordinates) are transformed by 4 x 4 floating-point matrices. Spatial coordinates are
projected from a position in the 3D world to a position on your screen.
Primitive Assembly: Clipping, a major part of primitive assembly, is the elimination
of portions of geometry which fall outside a half-space, defined by a plane. Point
clipping simply passes or rejects vertices; line or polygon clipping can add additional
vertices depending upon how the line or polygon is clipped[3].
Pixel Operations: While geometric data takes one path through the OpenGL rendering
pipeline, pixel data takes a different route. Pixels from an array in system memory are
first unpacked from one of a variety of formats into the proper number of
components. Next the data is scaled, biased, and processed by a pixel map. The results
are clamped and then either written into texture memory or sent to the rasterization
step.
Texture Assembly: An OpenGL application may wish to apply texture images onto
geometric objects to make them look more realistic. If several texture images are
used, it's wise to put them into texture objects so that you can easily switch among
them.
Rasterization: Rasterization is the conversion of both geometric and pixel data into
fragments. Each fragment square corresponds to a pixel in the framebuffer. Line and
polygon stipples, line width, point size, shading model, and coverage calculations to
support antialiasing are taken into consideration as vertices are connected into lines or
the interior pixels are calculated for a filled polygon. Color and depth values are
assigned for each fragment square[1].
Fragment Operations: The first operation which may be encountered is texturing,
where a texel is generated from texture memory for each fragment and applied to the
fragment. Then fog calculations may be applied, followed by the scissor test, the
alpha test, the stencil test, and the depth-buffer test. Failing an enabled test may end
the continued processing of a fragment's square. Then, blending, dithering, logical
operation, and masking by a bitmask may be performed. Finally, the thoroughly
processed fragment is drawn into the appropriate buffer, where it has finally advanced
to be a pixel and achieved its final resting place.
CHAPTER 2
REQUIREMENTS SPECIFICATION
2.1SOFTWARE REQUIREMENTS
Operating system – Windows 10
Microsoft Visual Studio 2013
OPENGL library files – GL, GLU, GLUT
Language used is C/C++
CHAPTER 3
SYSTEM DEFINITION
negative edge path costs, producing a shortest path tree. This algorithm is often used in routing and
other network related protocols. On executing the code, title of the project is displayed with the
details of submission.
The user can proceed in the project by clicking the right button from the mouse. A menu appears
which has the options like Read the cost matrix, display weighted graph, display shortest path, exit.
Based on clicking the options Read cost matrix, the user can give the input for number of nodes, the
cost matrix and specify the source to find the shortest from that source node. The nodes will be
displayed which allows the user to project the position for placing the node in input area, after
selecting the option display weighted graph, it will be displayed. Once the option display shortest
path is selected the shortest path graph is selected in output area, after choosing the option exit it
comes out of the program[10].
Algorithm:
This is pseudocode for Dijkstra's algorithm, mirroring Python syntax. It can be used in order
to implement the algorithm in any language.
if v ≠ source
dist[v]: = infinity // Unknown distance function from source to each node set to infinity
v: = vertex in Q with min dist[v] // In the first run-through, this vertex is the source node
remove v from Q
for each neighbour u of v: // where neighbour u has not yet been removed from Q.
return dist []
end function
The second implementation is time complexity wise better . And in Dijkstra’s algorithm, we
need a priority queue and below operations on priority queue:
ExtractMin: from all those vertices whose shortest distance is not yet found, we need to get
vertex with minimum distance.
Decrease Key: After extracting vertex we need to update distance of its adjacent vertices,
and if new distance is smaller, then update that in data structure.
The code calculates shortest distance and calculate the path information. We can create a
parent array, update the parent array when distance is updated (like prim’s implementation)
and use it show the shortest path from source to different vertices.
The code is for undirected graph, same Dijkstra function can be used for directed graphs
also.
The code finds shortest distances from source to all vertices. If we are interested only in
shortest distance from source to a single target, we can break the for loop when the picked
minimum distance vertex is equal to target
Dijkstra’s algorithm doesn’t work for graphs with negative weight edges. For graphs with
negative weight edges, Bellman–Ford algorithm can be used.
FLOW DIAGRAM
For any of the program flow chart is compulsory to understand the program.
We consider the flow chart for the texture project in which the flow starts from start and
proceeds to the main function after which it comes to the initialization of call back functions
and further it proceeds to mouse and keyboard functions after all the function, the flow comes
to quit which is the end of the flow chart.
Initialize the interaction with the windows. Initialize the display mode- double buffer
and depth buffer. Initialize the various callback functions for drawing and redrawing the
polygon, for mouse and keyboard interface, for movement of the image in different
directions. Initialize the window position and size and create the window to display the
output[9].
Functions used :
void title () : This function is used for displaying title page, Project topic, student
Name, class, section, usn,directing to the page where the algorithm is implemented.
void initial () : This function is used to create and initialize a menu which is
displayed when right button of the mouse is pressed.
void raster () : This function is used to draw bitmap number that is to assign the cost
to the edge.
void drawsquare() : This function is used to draw the nodes in form of a square
shape.
void read() :This function is uses to take the input data from the user which includes
number of vertices,cost matrix and source node.
void init(void) : This function is used to initialize the components of the window
created.
void drawline() : This function is used to draw edge connecting the nodes.
void shortestpath() : This function is used to implement the Dijkstras algorithm to
find the shortest path from the source node to all other remaining nodes.
void top_menu() :This function used to display the options after pressing right mouse
button.
void main(int argc, char **argv) : The main( ) function is where actual execution
of the program starts.
CHAPTER 4
IMPLEMENTATION
//FUNCTION TO DELAY
void delay ()
{
for (int i=0; i<12000; i++)
for (int j=0; j<12000; j++);
}
void title ()
{
glLineWidth (3.0);
glColor3f (1.0,1.0,0.0);
glBegin(GL_LINE_LOOP);
glVertex2f (10,10);
glVertex2f (10,490);
glVertex2f (490,490);
glVertex2f (490,10);
glEnd ();
set Font(GLUT_BITMAP_HELVETICA_18);
glColor3f (1.0,1.0,1.0);
drawstring (100,440,"Topic: Dijkstra’s Algorithm");
delay ();
glColor3f (1.0,1.0,1.0);
drawstring (100,400,"Submitted by");
delay ();
glColor3f (0.0,1.0,0.0);
drawstring (100,360,"Rashmi.C");
delay ();
glColor3f (0.0,1.0,0.0);
drawstring (100,320,"VI CSE B");
delay ();
glColor3f(0.0,1.0,0.0);
drawstring(100,280,"1RN19CS410");
delay ();
glColor3f (1.0,1.0,1.0);
drawstring (100,100,"Right click in My Window for options");
glFlush ();
}
void initial ()
{
glClear(GL_COLOR_BUFFER_BIT);
set Font(GLUT_BITMAP_HELVETICA_18);
glColor3f (0.0,0.0,0.0);
drawstring (20,230,"Input Area");
drawstring (20,470,"Output Area");
glColor3f(0.0,0.0,0.0);
glLineWidth (3.0);
glBegin(GL_LINES);
glVertex2f (10,10);
glVertex2f (10,490);
glVertex2f (10,490);
glVertex2f (490,490);
glVertex2f (490,490);
glVertex2f (490,10);
glVertex2f (490,10);
glVertex2f (10,10);
glVertex2f (10,250);
glVertex2f (490,250);
glEnd ();
glBegin(GL_LINES);
glVertex2f (10,250);
glVertex2f (490,250);
glEnd ();
glFlush ();
}
glFlush ();
if(i<=n)
{
if(i==src)
glColor3f(0.7f, 0.4f, 0.0f);
else
glColor3f(0.5f, 0.5f, 0.8f);
glBegin(GL_POINTS);
glVertex2f(x , y);
glEnd ();
a[i]=x;
b[i]=y;
glFlush ();
}
i=i+1;
}
void read ()
{
printf ("Enter the number of vertices\n");
scanf_s ("%d”, &n);
printf ("Enter the cost matrix\n");
for (int j=1; j<=n; j++)
for (int k=1; k<=n; k++)
{
scanf("%d",&cost[j][k]);
if(cost[j][k] ==0)
cost[j][k] =999;
}
void drawline ()
{
int j, k, x1, x2, y1, y2;
for (j=1; j<=n; j++)
{
for (k=1; k<=n; k++)
{
if(cost[j][k]! =999 && j<k)
{
x1=a[j];
y1=b[j];
x2=a[k];
y2=b[k];
glColor3f(0.0,0.5,0.0);
glLineWidth (3);
glBegin(GL_LINES);
glVertex2i(x1-7, y1+10);
glVertex2i(x2-7, y2+10);
glEnd ();
s1=itoa(cost[j][k], s,10);
drawstring((x1+x2-16)/2, (y1+y2+22)/2, s1);
glFlush ();
}
glColor3f(1.0,0.5,0.0);
glBegin(GL_LINES);
glVertex2i(x1+10, y1+18);
glVertex2i(x2+10, y2+18);
glEnd ();
s1=itoa(cost[j][k], s,10);
drawstring((x1+x2+20)/2,(y1+y2+36)/2,s1);
glFlush ();
}
}
}
}
//START OF DIJIKSTRA's
int w, j, u,v,k,p,q,x1,y1,x2,y2,x,y;
int dist[MAX], visit[MAX], parent[MAX], mincost, min;
for (w=1; w<=n; w++)
{
visit[w]=0;
dist[w]=cost[src][w];
parent[w]=src;
}
visit[src]=1;
mincost=0;
k=1;
visit[u]=1;
mincost=mincost+dist[u];
tree[k][1] =parent[u];
tree[k++] [2] =u;
if (w! =src)
printf("%d-->%d = %d\n”, src, w, dist[w]);
}
//END OF DIJIKSTRAS
glPointSize (25);
if(r==src)
glColor3f (0.7f, 0.4f, 0.0f);
else
glColor3f (0.5f, 0.5f, 0.8f);
glBegin(GL_POINTS);
glVertex2f (x, y+250);
glEnd ();
glColor3f(0.0,1.0,0.0);
glFlush ();
x1=a[p];
y1=b[p];
x2=a[q];
y2=b[q];
if(p<q)
{
glColor3f(0.0,0.5,0.0);
glBegin(GL_LINES);
glVertex2i(x1, y1+250);
glVertex2i(x2, y2+250);
glEnd ();
s1=itoa(cost[p][q], s,10);
else
{
glColor3f(1.0,0.5,0.0);
glBegin(GL_LINES);
glVertex2i (x1, y1+250);
glVertex2i (x2, y2+250);
glEnd ();
s1=itoa(cost[p][q],s,10);
drawstring((x1+x2)/2, (y1+y2+500)/2, s1);
}
}
glFlush ();
}
}
switch(option)
{
case 1:
read ();
glutPostRedisplay ();
break;
case 2:
drawline ();
glutPostRedisplay ();
break;
case 3:
shortest path ();
glutPostRedisplay ();
break;
case 4:
exit (0);
}
}
void myInit1()
{
glClearColor (0.0,0.0,0.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize (5.0);
gluOrtho2D (0.0,500.0,0.0,500.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity ();
set Font(GLUT_BITMAP_HELVETICA_18);
}
void display1(void)
{
glClear(GL_COLOR_BUFFER_BIT);
title ();
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition (820,100);
glutInitWindowSize (450,450);
glutCreateWindow ("Front Sheet");
glutDisplayFunc(display1);
myInit1();
glutInitDisplayMode (GLUT_SINGLE|GLUT_RGB );
glutInitWindowSize (500,500);
glutInitWindowPosition (0,0);
glutCreateWindow ("My Window");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutCreateMenu(top_menu);
glutMainLoop ();
}
CHAPTER 5
TESTNG AND RESULTS
5.2 Snapshots
5.1 Front page: The below screenshot displays the title page which has name of the project and
submitted details.
5.2 Read data: The below screenshot displays the input taken from the user.
5.3 Input: The below screenshot displays the weighted graph for the given input in input area.
5.4 Output: The below screenshot displays the shortest path for the given input in output area.
CONCLUSION
Dijkstra’s algorithm is very useful algorithm, especially if you need to find the shortest
between two places which is widely used in Google maps. Code and complexity of these algorithms
is bigger, but the tradeoff is ease of use. It can grow and shrink at runtime by allocating and
deallocating memory.
So, there is no need to give initial size for the distance matrix. Creating and display of nodes are
really an easier. In Dijkstra’s we just have to update the cost matrix and the source node to find the
path. As the size of the cost matrix is given by user, they can be for a larger graph or a small graph
so there is no predefined distance matrix .
The main advantage of this algorithm is that the node can be placed at required position by clicking
in the input area, to insert and specifying the path cost value in order to display the graph[4].
So Dijkstra’s algorithm is better compared to the other algorithms, because it has wide applications
like Shortest Path, geographical Maps, To find locations of Map which refers to vertices of
graph, Distance between the location refers to edges, used in IP routing to find Open shortest
Path First, used in the telephone network.and it is also called A* algorithm.
FUTURE ENHANCEMENT
This project has been designed using C++, which works on the windows platform. The
project can be designed using other languages and better graphical interfaces. The following
features could have been incorporated. The project has a very vast scope in future. The project can
be implemented on intranet in future. Project can be updated in near future as and when
requirement for the same arises, as it is very flexible in terms of expansion. With the proposed
software of database Space Manager ready and fully functional the client is now able to manage and
hence run the entire work in a much better, accurate and error free manner. The following are the
future scope for the project.
BIBLIOGRAPHY
References
[1] Donald Hearn & Pauline Baker: Computer Graphics with OpenGL Version,3rd / 4th
Edition, Pearson Education,2011
[2] Edward Angel: Interactive Computer Graphics- A Top Down approach with OpenGL, 5th
edition. Pearson Education, 2008
[3] James D Foley, Andries Van Dam, Steven K Feiner, John F Huges Computer graphics
with OpenGL: pearson education
[4] M M Raiker, Computer Graphics using OpenGL, Filip learning/Elsevier
[5] Kelvin Sung, Peter Shirley, steven Baer : Interactive Computer Graphics, concepts and
applications, Cengage Learning
[6] www.opengl.org
[7] www.vtupro.blogspot.in
[8] www.opengl.start4all.com
[9]https://practice.geeksforgeeks.org/problems/what-are-real-life-applications-of-dijkstras-
algorithm
[10] https://www.sciencedirect.com/science/article/pii/S1570866709000653
[11]http://www.iaarc.org/publications/2014_proceedings_of_the_31st_isarc_sydney_australia
/application_of_dijkstras_algorithm_in_the_smart_exit_sign.html