# Dijkstra in C

Posted: , Last Updated:

``````package dijkstra.c.simple;

import java.util.Arrays;

/**
* Used to perform the dijkstra Algorithm using adjacency matrices.
* For a faster implementation, see @see ../fast/Dijkstra.java (using adjacency Lists)
*/
public class Dijkstra {
/**
* Implementation of dijkstra using adjacency matrix.
* This returns an array containing the length of the shortest path from the start node to each other node.
* It is only guaranteed to return correct results if there are no negative edges in the graph. Positive cycles are fine.
* This has a runtime of O(|V|^2) (|V| = number of Nodes), for a faster implementation see @see ../fast/Dijkstra.java (using adjacency lists)
*
* @param graph an adjacency-matrix-representation of the graph where (x,y) is the weight of the edge or 0 if there is no edge.
* @param start the node to start from.
* @return an array containing the shortest distances from the given start node to each other node
*/
public static int[] dijkstra(int[][] graph, int start) {

//This contains the distances from the start node to all other nodes
int[] distances = new int[graph.length];
//Initializing with a distance of "Infinity"
Arrays.fill(distances, Integer.MAX_VALUE);
//The distance from the start node to itself is of course 0
distances[start] = 0;

//This contains whether a node was already visited
boolean[] visited = new boolean[graph.length];

//While there are nodes left to visit...
while (true) {

// ... find the node with the currently shortest distance from the start node...
int shortestDistance = Integer.MAX_VALUE;
int shortestIndex = -1;
for (int i = 0; i < graph.length; i++) {
//... by going through all nodes that haven't been visited yet
if (distances[i] < shortestDistance && !visited[i]) {
shortestDistance = distances[i];
shortestIndex = i;
}
}

System.out.println("Visiting node " + shortestDistance + " with current distance " + shortestDistance);

if (shortestIndex == -1) {
// There was no node not yet visited --> We are done
return distances;
}

//...then, for all neighboring nodes....
for (int i = 0; i < graph[shortestIndex].length; i++) {
//...if the path over this edge is shorter...
if (graph[shortestIndex][i] != 0 && distances[i] > distances[shortestIndex] + graph[shortestIndex][i]) {
//...Save this path as new shortest path.
distances[i] = distances[shortestIndex] + graph[shortestIndex][i];
System.out.println("Updating distance of node " + i + " to " + distances[i]);
}
}
// Lastly, note that we are finished with this node.
visited[shortestIndex] = true;
System.out.println("Visited nodes: " + Arrays.toString(visited));
System.out.println("Currently lowest distances: " + Arrays.toString(distances));

}
}
}
``````

# Dijkstra's Algorithm

The Dijkstra algorithm is an algorithm used to solve the shortest path problem in a graph. This means that given a number of nodes and the edges between them as well as the “length” of the edges (referred to as “weight”), the Dijkstra algorithm is finds the shortest path from the specified start node to all other nodes. Nodes are sometimes referred to as vertices (plural of vertex) - here, we’ll call them nodes.

## Description of the Algorithm

The basic principle behind the Dijkstra algorithm is to iteratively look at the node with the currently smallest distance to the source and update all not yet visited neighbors if the path to it via the current node is shorter. In more detail, this leads to the following Steps:

1. Initialize the distance to the starting node as 0 and the distances to all other nodes as infinite
2. Set all nodes to “unvisited”
3. While we haven’t visited all nodes:

1. Find the node with currently shortest distance from the source (for the first pass, this will be the source node itself)
2. For all nodes next to it that we haven’t visited yet, check if the currently smallest distance to that neighbor is bigger than if we were to go via the current node
3. If it is, update the smallest distance of that neighbor to be the distance from the source to the current node plus the distance from the current node to that neighbor

In the end, the array we used to keep track of the currently shortest distance from the source to all other nodes will contain the (final) shortest distances.

## Example of the Algorithm

Consider the following graph:

The steps the algorithm performs on this graph if given node 0 as a starting point, in order, are:

1. Visiting node 0
2. Updating distance of node 1 to 3
3. Updating distance of node 2 to 1
4. Visited nodes: 0
5. Currently lowest distances: [0, 3, 1, Infinite, Infinite, Infinite]
6. Visiting node 1 with current distance 1

• Updating distance of node 3 to 5
• Visited nodes: 0, 2
• Currently lowest distances: [0, 3, 1, 5, Infinite, Infinite]
7. Visiting node 3 with current distance 3

• Updating distance of node 4 to 4
• Visited nodes: 0, 1, 2
• Currently lowest distances: [0, 3, 1, 5, 4, Infinite]
8. Visiting node 4 with current distance 4

• Updating distance of node 5 to 5
• Visited nodes: 0, 1, 2, 4
• Currently lowest distances: [0, 3, 1, 5, 4, 5]
9. Visiting node 5 with current distance 5

• No distances to update
• Visited nodes: 0, 1, 2, 3, 4
• Currently lowest distances: [0, 3, 1, 5, 4, 5]
10. Visiting node 5 with current distance 5

• No distances to update
• Visited nodes: 0, 1, 2, 3, 4, 5

All nodes visited Final lowest distances: [0, 3, 1, 5, 4, 5]

## Runtime of the Algorithm

The runtime complexity of Dijkstra depends on how it is implemented. If a min-heap is used to determine the next node to visit, and adjacency is implemented using adjacency lists, the runtime is O(|E| + |V|log|V|) (|V| = number of Nodes, |E| = number of Edges). If a we simply search all distances to find the node with the lowest distance in each step, and use a matrix to look up whether two nodes are adjacent, the runtime complexity increases to O(|V|^2).

## Space of the Algorithm

The space complexity of Dijkstra depends on how it is implemented as well and is equal to the runtime complexity.

Dijkstra's Algorithm in other languages:

# C

C is a compiled language used for many purposes, although it can be primarily found in systems where importance is important. This is because C offers a lot of low-level support for optimization, at the cost of not having some of the convenient abstractions that other languages offer. C is therefore primarily found in situations where available computation power is low such as embedded systems, or situations where required computation power is high, such as simulation or deep learning.

## Getting to “Hello World” in C

The most important things first - here’s how you can run your first line of code in C.

1. If you’re on Linux or Mac, download and install the latest version of GCC, a C compiler, from gcc.gnu.org. You can also download an earlier version if your use case requires it.
2. If you’re on Windows, you can also install GCC, even though it might cause problems. You also have other options outlined e.g. here.
3. Open a terminal, make sure the `gcc` command is working (or the according command for whichever compiler you’re using), and that the command your’re going to be using is referring to the version you just installed by running `gcc --version`. If you’re getting a “command not found” error (or similar), try restarting your command line, and, if that doesn’t help, your computer. If the issue persists, here are some helpful forum questions for each platform:

4. As soon as that’s working, copy the following snippet into a file named HelloWorld.c:

``````#include<stdio.h>
int main() {
printf("Hello World\n");
return 0;
}``````
5. Change directory by typing `cd path/to/HelloWorld`, then run `gcc HelloWorld.c` to compile the file (which creates the bytecode), then run `./a.out`. This should print “Hello World” to your Terminal.

That’s it! People who know multiple programming languages will notice that the entry barrier in C is a little lower than Java even though it is lower-level, while the entry barrier to Python is lower even though it is higher-level. My personal observation is that low-level and high-level languages tend to have low barriers of entry, whereas mid-level languages have higher barriers.

## Fundamentals in C

To understand algorithms and technologies implemented in C, one first needs to understand what basic programming concepts look like in this particular language. Each of the following snippets should be compiled and run using the commands mentioned above.

### Variables and Arithmetic

Variables in C are statically typed, meaning the content of a variable needs to be specified when writing the code. The datatype for whole numbers, for example is `int`. Numbers with decimal places are typed `float` or `double` depending on the required precision. The type for text ist `String`.

``````#include<stdio.h>

int main() {
int number = 5;
double decimalNumber = 3.25;
double result = number * decimalNumber;
char callout [] = "The number is ";
// In this instance, the values are concatenated rather than added because one of them is a String.
printf("%s", callout);
printf("%f", result);
printf("\n");
return 0;
}``````

### Arrays

Arrays in C are real arrays (as opposed to e.g. Python where they’re implemented as lists). The implications of that are that the size needs to be set when they are created and cannot be changed, but also that they are more efficient in C than they are in Python. Also, contrary to Java, C does not check array bounds. If you access an index that doesn’t exist, the program will read whatever is in the memory at that location (which will probably be gibberish).

``````int integers;
integers = 12; // Assigning values to positions in the array
printf("%d\n", integers); // will be 0
printf("%d\n", integers); // will be 12
printf("%d\n", integers); // will print something random that happened to be at that location in memory
return 0;``````

### Conditions

Just like most programming languages, C can do `if-else` statements. Additionally, C can also do `switch-case` statements.

``````int value = 5;
if(value == 5){
printf("%s\n", "Value is 5");
} else if(value < 5){
printf("%s\n", "Value is less than 5");
} else {
printf("%s\n", "Value is something else");
}

switch (value){
case 1:
printf("%s\n", "Value is 1");
break; // Don't go further down the cases
case 2:
printf("%s\n", "Value is 2");
break; // Don't go further down the cases
case 3:
printf("%s\n", "Value is 3");
break; // Don't go further down the cases
case 4:
printf("%s\n", "Value is 4");
break; // Don't go further down the cases
case 5:
printf("%s\n", "Value is 5");
break; // Don't go further down the cases
default:
printf("%s\n", "Value is something else");
}``````

The above C code will print “Value is 5” twice.

### Loops

C supports `for`, `while` as well as `do while` loops. `break` and `continue` statements are also supported. The below example illustrates the differences:

``````int value = 2;
for (int i = 0; i < value; i++) {
printf("%d\n", i);
}
while (value > 0) {
printf("%d\n", value);
value--;
}
do {
printf("%d\n", value);
value--;
} while (value > 0);``````

This will print the following to the terminal:

``````0
1
2
1
0``````

Note the last `0`: it is printed because in the `do-while`-loop, compared to the `while`-loop. the code block is executed at least once before the condition is checked.

### Functions

Functions in C can be declared similar to Java, but require less boilerplate since they don’t need to be part of classes or objects. Here is a minimal example of a function:

``````#include<stdio.h>

int addNumbers(int numberOne, int numberTwo) {
return numberOne + numberTwo;
}

int main() {
printf("%d\n", addNumbers(3, 4));
}``````

### Syntax

C requires the use of curly brackets (`{}`) to surround code blocks in conditions, loops, functions etc.; It also requires semicolons at then end of statements. While this can lead to some annoying syntax errors, it also means the use of whitespace for preferred formatting (e.g. indentation of code pieces) does not affect the code. Note how the Syntax of C is very similar to Java. The Syntax of Java, and many other languages that came after and/or were derived from C copy many aspects of its Syntax.

## Advanced Knowledge of C

C was first released in 1972, is statically typed and was ported to many platforms with various implementations (one of which is GCC which was presented in this article). For more information, C has a great Wikipedia) article.