Applications, Advantages and Disadvantages of Directed Graph


Diverse domains, including CS, social networks, and logistics, use directed graphs, also known as digraphs. Arrows indicating the direction of links serve to depict the interconnections between the various components. They have the ability to represent intricate connections, handle data quickly, and facilitate pathfinding algorithms. Their drawbacks, however, include the potential for analysis complexity, the challenge of visualising vast graphs, and the requirement for cautious treatment of cyclic structures. Despite these drawbacks, directed graphs continue to be fundamental tools for comprehending, evaluating, and enhancing interconnected systems in a variety of real−world contexts.

Methods Used

  • Topological Sorting

  • Strongly Connected Components

Topological Sorting

A crucial graph procedure called topological sorting is used to arrange the nodes in a directed acyclic graph (DAG) according to their dependencies or precedence relationships. It enables us to arrange tasks or events in directed graphs so that each task follows all of its prerequisite tasks. This sorting supports planning and scheduling while also spotting circular dependencies. Usually, the method traverses the graph using depth−first search (DFS), which results in the sorted order. It begins by visiting a node and then iteratively investigates any unexplored neighbours. The current node is added to the topological order when all of its neighbours have been visited. This procedure is repeated until all vertices are represented, producing a workable series of operations or events.

Algorithm

  • Create a blank stack from scratch to store the topological order.

  • To keep track of visited nodes, create a boolean array with false as its initial value for each node.

  • Perform the following for every graph node that hasn't been visited:

    Invoke a DFS procedure on the node.

    When doing DFS:

    Add a visitation marker to the current node.

    Add a visitation marker to the current node.

    Process all of the current node's neighbours before pushing it to the stack.

  • The stack will maintain the topological order after visiting each node.

  • Get the final topological sorting by popping elements out of the stack.

Example

#include <iostream>
#include <vector>
#include <stack>
using namespace std;

void topologicalSort(vector<vector<int>>& graph, int node, vector<bool>& visited, stack<int>& result) {
    visited[node] = true;
    for (int neighbor : graph[node]) {
        if (!visited[neighbor]) {
            topologicalSort(graph, neighbor, visited, result);
        }
    }
    result.push(node);
}

vector<int> performTopologicalSort(vector<vector<int>>& graph, int numNodes) {
    vector<int> topologicalOrder;
    stack<int> result;
    vector<bool> visited(numNodes, false);
    
    for (int i = 0; i < numNodes; ++i) {
        if (!visited[i]) {
            topologicalSort(graph, i, visited, result);
        }
    }
    
    while (!result.empty()) {
        topologicalOrder.push_back(result.top());
        result.pop();
    }
    
    return topologicalOrder;
}

int main() {
    int numNodes = 6;
    vector<vector<int>> graph(numNodes);
    graph[2].push_back(3);
    graph[3].push_back(1);
    graph[4].push_back(0);
    graph[4].push_back(1);
    graph[5].push_back(0);
    graph[5].push_back(2);

    vector<int> sortedOrder = performTopologicalSort(graph, numNodes);
    
    cout << "Topological Sorting Order: ";
    for (int node : sortedOrder) {
        cout << node << " ";
    }
    cout << endl;
    
    return 0;
}

Output

Topological Sorting Order: 5 4 2 3 1 0 

Strongly Connected Components

In a directed graph, Strongly Connected Components (SCCs) are the basic units where every vertex can be reached from every other vertex in the component. To put it another way, each SCC creates a closed cycle or loop that ensures strong communication between its nodes. Finding bottlenecks in transportation networks, spotting connections in software modules, or sifting through social networks for closely−knit communities are just a few real−world applications where this idea is essential. The analysis of complicated systems is made easier by efficient algorithms like Tarjan's or Kosaraju's that can recognise SCCs and express them compactly. SCCs aid scientists and engineers in understanding the fundamental structure and behaviour of directed graphs by separating these cohesive subgraphs.

Algorithm

  • Enter the first and second numbers, respectively.

  • Summarise num1 and num2 and save the result in the sum variable.

  • Publish the sum's value.

Example

#include <iostream>
#include <vector>
#include <stack>
using namespace std;

void DFS1(int vertex, vector<vector<int>>& graph, vector<bool>& visited, stack<int>& stack) {
    visited[vertex] = true;
    for (int neighbor : graph[vertex]) {
        if (!visited[neighbor]) {
            DFS1(neighbor, graph, visited, stack);
        }
    }
    stack.push(vertex);
}

void DFS2(int vertex, vector<vector<int>>& transposedGraph, vector<bool>& visited, vector<int>& component) {
    visited[vertex] = true;
    component.push_back(vertex);
    for (int neighbor : transposedGraph[vertex]) {
        if (!visited[neighbor]) {
            DFS2(neighbor, transposedGraph, visited, component);
        }
    }
}

vector<vector<int>> findSCCs(vector<vector<int>>& graph, int numVertices) {
    stack<int> stack;
    vector<bool> visited(numVertices, false);
    for (int vertex = 0; vertex < numVertices; ++vertex) {
        if (!visited[vertex]) {
            DFS1(vertex, graph, visited, stack);
        }
    }

    vector<vector<int>> transposedGraph(numVertices);
    for (int vertex = 0; vertex < numVertices; ++vertex) {
        for (int neighbor : graph[vertex]) {
            transposedGraph[neighbor].push_back(vertex);
        }
    }

    vector<vector<int>> SCCs;
    visited.assign(numVertices, false);
    while (!stack.empty()) {
        int vertex = stack.top();
        stack.pop();
        if (!visited[vertex]) {
            vector<int> component;
            DFS2(vertex, transposedGraph, visited, component);
            SCCs.push_back(component);
        }
    }

    return SCCs;
}

int main() {
    // Example directed graph
    int numVertices = 6;
    vector<vector<int>> graph = {
        {1, 2},
        {2},
        {3, 4},
        {0, 5},
        {5},
        {4}
    };

    vector<vector<int>> SCCs = findSCCs(graph, numVertices);
    for (const auto& SCC : SCCs) {
        for (int vertex : SCC) {
            cout << vertex << " ";
        }
        cout << "\n";
    }

    return 0;
}

Output

0 3 2 1 
5 4 

Applications

  • Directed graphs are used to simulate social networks, in which nodes stand in for people or other objects and directed edges show the direction of connections (such as friendships, followers, etc.).

  • They are used to depict computer networks, with nodes acting as devices and edges acting as paths for data flow, assisting in network analysis and optimisation.

  • In order to ensure appropriate sequencing and prevent circular dependencies, directed graphs are used in software and project management to express relationships between tasks or modules.

  • Route planning and navigation are made easier by the use of directed graphs, which can help locate the shortest or quickest route between two points.

  • Directed graphs are used in the building of compilers to describe the data and control flow between different programme components.

  • Workflow modelling enables effective task scheduling and resource allocation by modelling complicated workflows in corporate processes.

  • Directed graphs are used in game theory to examine how players strategically interact in games involving consecutive moves.

  • Web page ranking is done using directed graphs (web graphs), which are used by search engines like Google to assign a pageRank−like value to each web page.

  • State Machines: State transitions in finite state machines, which are frequently used in digital systems, protocol design, and automation, are modelled using directed graphs.

  • Directed networks represent user−item interactions and make pertinent item suggestions based on graph traversal and analysis, which helps create recommendation systems.

Advantages

  • Directed edges, which indicate one−way connections between things, make it possible to accurately describe asymmetric interactions, such as those found in social networks or data flow in computer networks.

  • Directed graphs are used in pathfinding algorithms like Dijkstra's or Bellman−Ford to determine the shortest paths or best routes in logistics and transportation, enabling effective route planning.

  • Directed graphs can help manage dependencies between modules or packages in software development, making it easier to comprehend the structure of the project and assuring appropriate build sequence.

  • Process modelling: Directed graphs are used in business process management to model and analyse workflows, assisting in the identification of bottlenecks and maximising productivity.

  • Organisational charts and class inheritance in object−oriented programming are examples of hierarchical structures that are represented by directed acyclic graphs (DAGs).

  • Knowledge representation systems, semantic networks, and decision trees all use directed graphs to efficiently organise and retrieve data.

  • In game theory, directed graphs are used to simulate games and tactics, such as the analysis of adversarial situations, electoral processes, and network games.

  • Systems that connect people and items based on their preferences and interactions are known as directed graphs recommendation systems.

  • Generally speaking, directed graphs offer a strong and adaptable tool for expressing, analysing, and comprehending complicated interactions and dependencies in a variety of areas.

Disadvantages

  • Complexity: Analysing and processing directed networks becomes exponentially more difficult as the number of nodes and edges rises. The computation required to find particular pathways, cycles, or patterns can be substantial.

  • Visualization Challenges: Large directed graphs can be challenging to visualise because of the many arrows and overlapping edges, which make it difficult to understand the overall structure.

  • Cyclic Structures: Cycles in a directed graph can result in infinite loops, which complicates some algorithms and computations.

  • Lack of Symmetry: Unlike undirected networks, directed graphs do not have symmetric connections, which might make it difficult to perform certain analysis and use symmetrical algorithms.

  • Data Integrity: In some cases, directed graphs may not accurately depict the dependencies or relationships that exist in reality, which could result in inaccurate conclusions or interpretations.

  • Implementation Complexity: Compared to simpler data structures, directed network algorithms might be more difficult to create and maintain.

Conclusion

In conclusion, because they can depict intricate dependencies and relationships, directed graphs, also known as digraphs, find extensive use in many different domains. In fields like computer science, social networks, and logistics where an understanding of interconnected systems is essential, they are especially useful. Pathfinding algorithms, effective data processing, and correct representation of asymmetric interactions are some benefits of directed graphs. However, they have some significant limitations, including analysis complexity, difficulties in visualising big graphs, and the potential for cyclic formations. Despite these drawbacks, directed graphs continue to be essential tools for assessing and improving interconnected systems in practical settings, promoting better problem−solving and decision−making.

Updated on: 02-Aug-2023

363 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements