Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 34

VISVESVARAYA TECHNOLOGICAL UNIVERSITY

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

Under the Guidance of:

Mrs. S Mamatha Jajur


Assistant Professor
Department of CSE

Department of Computer Science and Engineering


RNS Institute of Technology
Channasandra, Dr. Vishnuvardhan Road, Bengaluru-560 098
2020-2021
RNS Institute of Technology
Channasandra, Dr.Vishnuvardhan Road,
Bengaluru-560 098

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

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.

Mrs. S Mamatha Jajur Dr. Kiran P


Assistant Professor Head of Department
Dept. of CSE Dept. of CSE

External Viva:

Name of the Examiners Signature with Date

1.
2.
ACKNOWLEDGMENT

Any achievement, be it scholastic or otherwise does not depend solely on the


individual
efforts but on the guidance, encouragement and cooperation of intellectuals, elders and
friends. A number of personalities, in their own capacities have helped us in carrying out this
project work. We would like to take this opportunity to thank them all.

We would like to thank Dr. H N Shivashankar, Director, RNSIT, Bangalore, for his
moral support towards completing our project.

We are grateful to Dr. M K Venkatesha, Principal, RNSIT, Bangalore, for his


support towards completing this mini 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 deeply express my sincere gratitude to all my guides Mrs. S Mamatha Jajur,


Mrs. Ramyashree A N and Mrs. Swathi G, Assistant Professors, Department of CSE,
RNSIT, Bangalore, for their able guidance, regular source of encouragement and assistance
throughout this project.

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

This Project is on “DIJKSTRA’S ALGORITHM” Computer Graphics using


OpenGL Functions. It is a User interactive program where in the User can view the required
display by making use of the input devices like Keyboard and Mouse. This project mainly
consists of a nodes and lines which helps to view the shortest path and connectivity between
nodes. Options such as Read cost matrix ,Display weighted graph ,Display shortest path is
provided. The user can choose the desired option. It has a very simple and effective user
interface
The Dijkstra method is a well-known algorithm for finding the optimum path in
shortest-path search problems. With that method, however, the time required to find the
optimum path becomes remarkably long when the search scope is broad, so the Djikstra
method is not suitable for real-time problems. In this paper, we propose a method for
obtaining, in a short time, a path that is as close as possible to the path obtained by the
Dijkstra method (the optimum path). The new method extends the conventional Dijkstra
method so as to obtain a solution to a problem given within a specified time, such as path
search in a car navigation system. The effectiveness of that extended method is described
through use of simulations.

OpenGL(open graphics library) is a standard specification defining a cross language


cross platform API for writing applications that produce 2D and 3D computer graphics. The
interface consists of over 250 different function calls which can be used to draw complex 3D
scenes from simple primitives. OpenGL was developed by silicon graphics Inc.(SGI) in 1992
and is widely used in CAD ,virtual reality , scientific visualization , information visualization
and flight simulation. It is also used in video games, where it competes with direct 3D on
Microsoft Windows platforms.
CONTENTS
No. Chapters Page no.

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

1.1 INTRODUCTION TO COMPUTER GRAPHICS


Computer Graphics is concerned with all aspects of producing pictures or images
using a computer.
Applications of Computer Graphics
1. Display of information
2. Design
3. Simulation and animation
4. User interfaces
The Graphics Architecture
Graphics Architecture can be made up of seven components:
1. Display processors
2. Pipeline architectures
3. The graphics pipeline
4. Vertex processing
5. Clipping and primitive assembly
6. Rasterization
7. Fragment processing

Components of Graphics Architecture and their working


 Input: Graphics make use of various devices for data input. Most systems have a
Keyboard and one or more additional devices specifically designed for interactive
input. These include a mouse, trackball, space ball and joystick. Some other input
devices used in particular applications are digitizers, dials, image scanners and voice
systems.
 Devices: Here device may be Input device or Output device. Any device that allows
information from outside the computer to be communicated to the computer is
considered as Input device. Any device which is used to send data from a computer to
another device or user.

Dept. of CSE, RNSIT 2020-21 1


Dijkstra’s Algorithm

 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.

Dept. of CSE, RNSIT 2020-21 2


Dijkstra’s Algorithm

1.2 INTRODUCTION TO OPENGL


OpenGL is software used to implement computer graphics. The structure of OpenGL is
similar to that of most modern APIs including Java 3D and DirectX. OpenGL is easy to learn,
compared with other.

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

 OpenGL (Open Graphics Library) is the computer industry's standard application


program interface (API) for defining 2-D and 3-D graphic images.
 The OpenGL Utility Library (GLU) was a computer graphics library for OpenGL. It
consists of a number of functions that use the base OpenGL library to provide higher-
level drawing routines from the more primitive routines that OpenGL provides[5].

Dept. of CSE, RNSIT 2020-21 3


Dijkstra’s Algorithm

 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].

Most implementations of OpenGL have a similar order of operations, a series of processing


stages called the OpenGL rendering pipeline. This ordering, as shown in Figure 1.3, is not a
strict rule of how OpenGL is implemented but provides a reliable guide for predicting what
OpenGL will do. The following diagram shows the assembly line approach, which OpenGL
takes to process data. Geometric data (vertices, lines, and polygons) follow the path through
the row of boxes that includes evaluators and per-vertex operations, while pixel data (pixels,
images, and bitmaps) are treated differently for part of the process. Both types of data
undergo the same final steps (rasterization and per-fragment operations) before the final pixel
data is written into the frame buffer.

Dept. of CSE, RNSIT 2020-21 4


Dijkstra’s Algorithm

 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.

Dept. of CSE, RNSIT 2020-21 5


Dijkstra’s Algorithm

 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

Dept. of CSE, RNSIT 2020-21 6


Dijkstra’s Algorithm

2.1SOFTWARE REQUIREMENTS
 Operating system – Windows 10
 Microsoft Visual Studio 2013
 OPENGL library files – GL, GLU, GLUT
 Language used is C/C++

2.2 HARDWARE REQUIREMENTS


 Processor - Pentium Pro
 Memory - 128MB RAM
 500GB Hard Disk Drive
 Mouse or another pointing device
 Keyboard
 Display device
.

CHAPTER 3
SYSTEM DEFINITION

The project demonstrates the functioning of Dijkstra’s algorithm. Dijkstra’s Algorithm is a


graph search algorithm that solves the single-source shortest path problem for a graph with non-

Dept. of CSE, RNSIT 2020-21 7


Dijkstra’s Algorithm

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.

function Dijkstra (Graph, source):

dist[source]: = 0 // Distance from source to source is set to 0

for each vertex v in Graph: // Initializations

if v ≠ source

dist[v]: = infinity // Unknown distance function from source to each node set to infinity

add v to Q // All nodes initially in Q

while Q is not empty: // The main loop

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.

alt: = dist[v] + length (v, u)

Dept. of CSE, RNSIT 2020-21 8


Dijkstra’s Algorithm

if alt < dist[u]: // A shorter path to u has been found

dist[u]: = alt // Update distance of u

return dist []

end function

Step Wise Design


This project has used basic programming elements of OpenGL. Here we used
Dijkstra’s algorithm and modified it to single source to any destination (as opposed to any
source and all the destination).
The distance matrix, is not fixed in the code, to make it dynamic was just another step, but
again, we kept it low. When the program is run, it asks the number of vertices and cost matrix
from the users and then to select the source and a destination and later step to find the shortest
path between the given source and the destination. The packets that are designed here are so
generated to get the slope of the nodes and then traverse accordingly.

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.

Dept. of CSE, RNSIT 2020-21 9


Dijkstra’s Algorithm

FLOW DIAGRAM

Figure 3.1: Flowchart for implementation of Dijkstra’s Algorithm

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].

Dept. of CSE, RNSIT 2020-21 10


Dijkstra’s Algorithm

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.

Dept. of CSE, RNSIT 2020-21 11


Dijkstra’s Algorithm

CHAPTER 4
IMPLEMENTATION

4.1 Source Code


#define _CRT_SECURE_NO_WARNINGS
#define _CRT_NONSTDC_NO_DEPRECATE
#include<process.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include<math.h>
#include<stdio.h>
#include<windows.h>
#define GL_PI 3.14
#define MAX 25
int n, i=1, a[25], b[25],cost[25][25],tree[25][25],src,l[2],dist[10];
char s[20], *s1;
void *current font;

//FUNCTION TO SELECT BITMAP FONT

void set Font (void *font)


{
current font=font;
}

//FUNCTION TO DRAW BITMAP string at (x, y)

void drawstring (GLfloat x, GLfloat y,const char *string)


{
const char *c;
glRasterPos2f(x, y);

for (c=string; *c!='\0';*c++)


{
glutBitmapCharacter (current font, *c);
}
}

//FUNCTION TO DELAY

void delay ()
{
for (int i=0; i<12000; i++)
for (int j=0; j<12000; j++);
}

//DISPLAY FUNCTION FOR TITLE PAGE

Dept. of CSE, RNSIT 2020-21 12


Dijkstra’s Algorithm

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 ();
}

//DISPLAY FUNCTION FOR INITIALIZING (DRAWING) THE INPUT AND


OUTPUT //AREAS

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");

Dept. of CSE, RNSIT 2020-21 13


Dijkstra’s Algorithm

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 ();
}

//BLANK DISPLAY FUNCTION

void display (void)


{

glFlush ();

//DRAW A BITMAP NUMBER i at (x, y)

void raster (int x, int y, int i)


{
char z=i+'0';
glRasterPos2f (x-5, y-5);
glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, z);
}

//DRAW THE NODES (SQUARES)

void draw Square (int x, int y)


{

if(i<=n)
{

Dept. of CSE, RNSIT 2020-21 14


Dijkstra’s Algorithm

y = 500-y; //Convert from screen coordinates


glPointSize (40);

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;

glColor3f (0.0f, 1.0f, 0.0f);


s1=itoa (i, s,10);
drawstring (x-5, y-5,s1);

glFlush ();
}
i=i+1;
}

//READ DATA: |V|, COST MATRIX, SOURCE VERTEX

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;
}

printf ("Enter the source\n");


scanf_s ("%d”, &src);

printf ("\nGO TO MY WINDOW AND CLICK RIGHT BUTTON FOR NEXT


OPTION\n");
initial (); //Draw the initial screen
}

//DRAW THE EDGES

void drawline ()

Dept. of CSE, RNSIT 2020-21 15


Dijkstra’s Algorithm

{
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 ();
}

if(cost[j][k]! =cost[k][j] && cost[j][k]! =999 && j>k)


{
x1=a[j];
y1=b[j];
x2=a[k];
y2=b[k];

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 ();
}
}
}
}

void shortest path ()

Dept. of CSE, RNSIT 2020-21 16


Dijkstra’s Algorithm

//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;

for (w=1; w<n; w++)


{
min=999;
u=-1;

for (j=1; j<=n; j++)


{
if (! visit[j]&&dist[j]<min)
{
min=dist[j];
u=j;
}
}

visit[u]=1;
mincost=mincost+dist[u];

tree[k][1] =parent[u];
tree[k++] [2] =u;

for (v=1; v<=n; v++)


{
if (! visit[v]&&(cost[u][v]+dist[u]<dist[v]))
{
dist[v]=dist[u]+cost[u][v];
parent[v]=u;
}
}
}

printf ("\nThe cost from source to other vertices are\n\n");


for (w=1; w<=n; w++)
{

Dept. of CSE, RNSIT 2020-21 17


Dijkstra’s Algorithm

if (w! =src)
printf("%d-->%d = %d\n”, src, w, dist[w]);
}

//END OF DIJIKSTRAS

for (int r=1; r<=n; r++)


{
x=a[r];
y=b[r];

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);

s1=itoa (r, s,10);


drawstring (x, y+250, s1);

glFlush ();

for (int x=1; x<n; x++)


{
p=tree[x][1];
q=tree[x][2];

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);

Dept. of CSE, RNSIT 2020-21 18


Dijkstra’s Algorithm

drawstring((x1+x2)/2, (y1+y2+500)/2, s1);


}

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 ();
}
}

void mouse(int bin, int state , int x , int y)


{
if(bin==GLUT_LEFT_BUTTON&&state==GLUT_DOWN)
draw Square (x, y);

void top_menu (int option)


{

switch(option)
{
case 1:
read ();
glutPostRedisplay ();
break;
case 2:
drawline ();
glutPostRedisplay ();
break;
case 3:
shortest path ();
glutPostRedisplay ();
break;
case 4:
exit (0);
}
}

Dept. of CSE, RNSIT 2020-21 19


Dijkstra’s Algorithm

void init (void)


{
glClearColor (1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glViewport (0,0, 500, 500);
glMatrixMode (GL_PROJECTION);
glOrtho (0.0, 500.0, 0.0, 500.0, 1.0, -1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity ();
glFlush ();
}

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 ();

int main (int argc, char** argv)


{
glutInit (&argc, argv);

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);

Dept. of CSE, RNSIT 2020-21 20


Dijkstra’s Algorithm

glutAddMenuEntry ("Read Cost Matrix",1);


glutAddMenuEntry ("Display Weighted Graph",2);
glutAddMenuEntry ("Display Shortest Path",3);
glutAddMenuEntry("Exit",4);
glutAttachMenu(GLUT_RIGHT_BUTTON);
printf("\nGO TO MY WINDOW AND CLICK RIGHT BUTTON FOR NEXT
OPTION\n");
init();

glutMainLoop ();
}

Dept. of CSE, RNSIT 2020-21 21


Dijkstra’s Algorithm

CHAPTER 5
TESTNG AND RESULTS

5.1 Different types of testing


1. Unit Testing
Individual components are tested to ensure that they operate correctly. Each component is
tested independently, without other system components.
2. Module Testing
A module is a collection of dependent components such as a object class, an abstract data
type or some looser collection of procedures and functions. A module related components, so can
be tested without other system modules.
3. System Testing
This is concerned with finding errors that result from unanticipated interaction between sub-
system interface problems.
4. Acceptance Testing
The system is tested with data supplied by the system customer rather than simulated test
data.

Dept. of CSE, RNSIT 2020-21 22


Dijkstra’s Algorithm

5.2 Snapshots
5.1 Front page: The below screenshot displays the title page which has name of the project and
submitted details.

Figure 5.2.1: Title page

5.2 Read data: The below screenshot displays the input taken from the user.

Figure 5.2.3: Read cost matrix

5.3 Input: The below screenshot displays the weighted graph for the given input in input area.

Dept. of CSE, RNSIT 2020-21 23


Dijkstra’s Algorithm

Figure 5.2.4: Display weighted graph

5.4 Output: The below screenshot displays the shortest path for the given input in output area.

Figure 5.2.5: Display shortest path

Dept. of CSE, RNSIT 2020-21 24


Dijkstra’s Algorithm

Dept. of CSE, RNSIT 2020-21 25


Dijkstra’s Algorithm

Dept. of CSE, RNSIT 2020-21 26


Dijkstra’s Algorithm

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

Dept. of CSE, RNSIT 2020-21 27


Dijkstra’s Algorithm

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.

 Circular and priority queues can be implemented.


 Different shading effects can be added.
 We can give transparency and fogging to the objects.
This project can be further developed to find the shortest distance between 2 endpoints,
which is widely used in Google maps for navigation purpose.

BIBLIOGRAPHY

References

[1] Donald Hearn & Pauline Baker: Computer Graphics with OpenGL Version,3rd / 4th
Edition, Pearson Education,2011

Dept. of CSE, RNSIT 2020-21 28


Dijkstra’s Algorithm

[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

Dept. of CSE, RNSIT 2020-21 29

You might also like