# What are Graphs?

A Graph is a non-linear data structure consisting of nodes and edges. The nodes are sometimes also referred to as vertices and the edges are lines or arcs that connect any two nodes in the graph. More formally a Graph can be defined as,

A Graph consists of a finite set of vertices(or nodes) and a set of edges that connect a pair of nodes.

# Real-World Uses of Graph

Graphs are used in diverse industries and fields:

• GPS systems and Google Maps use graphs to find the shortest path from one destination to another.
• Social Networks use graphs to represent connections between users.
• The Google Search algorithm uses graphs to determine the relevance of search results.
• Operations Research is a field that uses graphs to find the optimal path to reduce the cost of transportation and delivery of goods and services.
• Even Chemistry uses graphs to represent molecules- Periodic Tables

# Graph Data Structure

Mathematical graphs can be represented in the data structure. We can represent a graph using an array of vertices and a two-dimensional array of edges. Before we proceed further, let’s familiarize ourselves with some important terms −

• Vertex − Each node of the graph is represented as a vertex. In the following example, the labeled circle represents vertices. Thus, A to G are vertices. We can represent them using an array as shown in the following image. Here A can be identified by index 0. B can be identified using index 1 and so on.
• Edge − Edge represents a path between two vertices or a line between two vertices. In the following example, the lines from A to B, B to C, and so on represent edges. We can use a two-dimensional array to represent an array as shown in the following image. Here AB can be represented as 1 at row 0, column 1, BC as 1 at row 1, column 2, and so on, keeping other combinations as 0.
• Adjacency − Two nodes or vertices are adjacent if they are connected to each other through an edge. In the following example, B is adjacent to A, C is adjacent to B, and so on.
• Path − Path represents a sequence of edges between the two vertices. In the following example, ABCD represents a path from A to D.

# Depth-first search

A depth-first search (DFS) is an algorithm for traversing a finite graph. DFS visits the child vertices before visiting the sibling vertices; that is, it traverses the depth of any particular path before exploring its breadth. A stack (often the program’s call stack via recursion) is generally used when implementing the algorithm.

DFS is the basis for many graph-related algorithms, including topological sorts and planarity testing.

`procedure DFS(G, v) is    label v as explored    for all edges e in G.incidentEdges(v) do        if edge e is unexplored then            w ← G.adjacentVertex(v, e)            if vertex w is unexplored then                label e as a discovered edge                recursively call DFS(G, w)            else               label e as a back edge`

A breadth-first search (BFS) is another technique for traversing a finite graph. BFS visits the sibling vertices before visiting the child vertices, and a queue is used in the search process. This algorithm is often used to find the shortest path from one vertex to another.

`procedure BFS(G, v) is    create a queue Q    enqueue v onto Q    mark v    while Q is not empty do        w ← Q.dequeue()        if w is what we are looking for then            return w        for all edges e in G.adjacentEdges(w) do            x ← G.adjacentVertex(w, e)            if x is not marked then                mark x                enqueue x onto Q    return null`

# Shortest Path Problems

The Single-Source Shortest Path (SSSP) problem consists of finding the shortest paths between a given vertex v and all other vertices in the graph. Algorithms such as Breadth-First-Search (BFS) for unweighted graphs or Dijkstra solve this problem. The All-Pairs Shortest Path (APSP) problem consists of finding the shortest path between all pairs of vertices in the graph. To solve this second problem, one can use the Floyd-Warshall algorithm or apply the Dijkstra algorithm to each vertex in the graph.

# What is Djiskstra Algorithm?

Dijkstra’s algorithm is one of the most popular algorithms for solving many single-source shortest path problems having non-negative edge weight in the graphs i.e., it is to find the shortest distance between two vertices on a graph. It was conceived by computer scientist Edsger W. Dijkstra in 1956 and published three years later.

Dijkstra’s Algorithm has several real-world use cases, some of which are as follows:

1. Digital Mapping Services in Google Maps: Many times we have tried to find the distance in G-Maps, from one city to another, or from your location to the nearest desired location. There encounters the Shortest Path Algorithm, as there are various routes/paths connecting them but it has to show the minimum distance, so Dijkstra’s Algorithm is used to find the minimum distance between two locations along the path. Consider India as a graph and represent a city/place with a vertex and the route between two cities/places as an edge, then by using this algorithm, the shortest routes between any two cities/places or from one city/place to another city/place can be calculated.
2. Social Networking Applications: In many applications, you might have seen the app suggests the list of friends that a particular user may know. How do you think many social media companies implement this feature efficiently, especially when the system has over a billion users. The standard Dijkstra algorithm can be applied using the shortest path between users measured through handshakes or connections among them. When the social networking graph is very small, it uses standard Dijkstra’s algorithm along with some other features to find the shortest paths, and however, when the graph is becoming bigger and bigger, the standard algorithm takes a few several seconds to count and alternate advanced algorithms are used.
3. Telephone Network: As we know, in a telephone network, each line has a bandwidth, ‘b’. The bandwidth of the transmission line is the highest frequency that that line can support. Generally, if the frequency of the signal is higher in a certain line, the signal is reduced by that line. Bandwidth represents the amount of information that can be transmitted by the line. If we imagine a city to be a graph, the vertices represent the switching stations, and the edges represent the transmission lines and the weight of the edges represents ‘b’. So as you can see it can fall into the category of shortest distance problem, for which the Dijkstra is can be used.
4. IP routing to find Open shortest Path First: Open Shortest Path First (OSPF) is a link-state routing protocol that is used to find the best path between the source and the destination router using its own Shortest Path First. Dijkstra’s algorithm is widely used in the routing protocols required by the routers to update their forwarding table. The algorithm provides the shortest cost path from the source router to other routers in the network.
5. Flighting Agenda: For example, If a person needs software for making an agenda of flights for customers. The agent has access to a database with all airports and flights. Besides the flight number, origin airport, and destination, the flights have departure and arrival times. Specifically, the agent wants to determine the earliest arrival time for the destination given an origin airport and start time. There this algorithm comes into use.
6. Designate file server: To designate a file server in a LAN(local area network), Dijkstra’s algorithm can be used. Consider that an infinite amount of time is required for transmitting files from one computer to another computer. Therefore to minimize the number of “hops” from the file server to every other computer on the network the idea is to use Dijkstra’s algorithm to minimize the shortest path between the networks resulting in the minimum number of hops.
7. Robotic Path: Nowadays, drones and robots have come into existence, some of which are manual, some automated. The drones/robots which are automated and are used to deliver the packages to a specific location or used for a task are loaded with this algorithm module so that when the source and destination are known, the robot/drone moves in the ordered direction by following the shortest path to keep delivering the package in a minimum amount of time.

--

-- ## Rishabh

Student from B.tech 2nd Year, A proud ARTH learner, love new technologies, Curious about many thing, likes to explore places, love eating pizza and much more.