Found 33676 Articles for Programming

C++ Program to Find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs)

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

2K+ Views

This is a C++ program to find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs) using Dijkstra Algorithm to find out from the first node in graph to every other node with the shortest path length showed beside each pair of vertices.AlgorithmBegin    Take the elements of the graph as input.    function shortestpath():    Initialize the variables    a[i] = 1    d[i] = 0    s[i].from = 0    Initialize a loop for i = 0 to 3 do       if b[0][i] == 0          continue       else   ... Read More

C++ Program to Find Path Between Two Nodes in a Graph

Farhan Muhamed
Updated on 30-Jul-2025 15:36:29

937 Views

In this article, we will learn how to find a path between two nodes in an undirected graph and implement it in C++. We will represent graph as an adjacency list and use a depth-first search (DFS) algorithm to find the path. We already discussed finding a path between two nodes in a directed graph, check here. Finding Path Between Two Nodes in an Undirected Graph We have an adjacency list representation of an undirected graph adj[] and two nodes src and dest, our task is to write a program that finds the path between the src ... Read More

C++ Program to Find Number of Articulation points in a Graph

Farhan Muhamed
Updated on 17-Jul-2025 17:17:46

591 Views

An articulation point is a vertex of the graph, which when removed, will break the graph into two or more components. You are given a connected undirected graph with V vertices and E edges. Your task is to find number of articulation points in the graph. To understand this better, consider the following example: Graph: 0 / \ 1 - 2 | 3 - 4 Output: 2 Articulation Points Explanation: Removing vertex 1 or 3 will split the graph into two components. Methods to Find Articulation Points To ... Read More

C++ Program to Find Chromatic Index of Cyclic Graphs

Farhan Muhamed
Updated on 16-Jul-2025 18:20:22

644 Views

The chromatic index of a graph is the maximum number of color needed for the edge coloring of the graph, such that no two adjacent edges have the same color. It is also known as the edge chromatic number. A cyclic graph is a graph that contains at least one cycle or a closed path. For example, a triangle is a cyclic graph and we can color its edges with 3 colors. // Graph: 0 -> 1 1 -> 2, 3 2 -> 0 Chromatic Index: 3 To find the chromatic index of cyclic graphs, we ... Read More

C++ Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

286 Views

Weakly or Strongly Connected for a given a directed graph can be find out using DFS. This is a C++ program of this problem.Functions usedBegin    Function fillorder() = fill stack with all the vertices.    a) Mark the current node as visited and print it    b) Recur for all the vertices adjacent to this vertex    c) All vertices reachable from v are processed by now, push v to Stack End Begin    Function DFS() :    a) Mark the current node as visited and print it    b) Recur for all the vertices adjacent to this vertex ... Read More

C++ Program to Check Whether a Hamiltonian Cycle or Path Exists in a Given Graph

Paul Richard
Updated on 30-Jul-2019 22:30:25

1K+ Views

A Hamiltonian cycle is a Hamiltonian Path such that there is an edge (in graph) from the last vertex to the first vertex of the Hamiltonian Path. It is in an undirected graph is a path that visits each vertex of the graph exactly once.Functions and purposes:Begin    1.function isSafe() is used to check for whether it is adjacent to the previously added vertex and already not added.    2. function hamiltonianCycle() solves the hamiltonian problem.    3. function hamCycle() uses hamiltonianCycle() to solve the hamiltonian problem.       It returns false if there is no Hamiltonian Cycle possible, ... Read More

C++ Program to Apply DFS to Perform the Topological Sorting of a Directed Acyclic Graph

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

1K+ Views

Topological sorting of DAG (Directed Acyclic Graph) is a linear ordering of vertices such that for every directed edge uv, where vertex u comes before v in the ordering. If the graph is not a DAG, Topological Sorting for a graph is not possible.Functions and pseudocodesBegin    function topologicalSort():    a) Mark the current node as visited.    b) Recur for all the vertices adjacent to this vertex.    c) Push current vertex to stack which stores result. End Begin    function topoSort() which uses recursive topological sort() function:    a) Mark all the vertices which are not visited.   ... Read More

C++ Program to Find Inverse of a Graph Matrix

Farhan Muhamed
Updated on 16-Jul-2025 16:24:13

9K+ Views

Every graph can be represented by a matrix called the adjacency matrix, in which each element indicates whether pairs of vertices have an edge between them. In this article, we will learn to find the inverse of a adjacency matrix of a graph using C++. First of all, let's understand what the inverse of a matrix means. Inverse of a Matrix The inverse of a matrix A is another matrix, denoted as A-1, such that the result of matrix multiplication A.A-1 will be an identity matrix I. The identity matrix is a square matrix with ones on the diagonal ... Read More

C++ Program to Check whether Graph is a Bipartite using DFS

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

271 Views

A bipartite graph is a graph in which if the graph coloring is possible using two colors i.e.; vertices in a set are colored with the same color. This is a C++ program to Check whether a graph bipartite or not using DFS.AlgorithmBegin    1. An array color[] is used to stores 0 or 1 for every node which denotes opposite colors.    2. Call function DFS from any node.    3. If the node w has not been visited previously, then assign !       color[v] to color[w] and call DFS again to visit nodes connected to w. ... Read More

C++ Program to Check whether Graph is a Bipartite using BFS

Farhan Muhamed
Updated on 17-Jul-2025 17:17:06

802 Views

The Breadth First Search (BFS) algorithm can be used to check whether a graph is bipartite by coloring the graph using two colors. This section will discuss how the BFS traversal can be used to check if a graph is bipartite. First of all, let's understand what a bipartite graph is. What is Bipartite Graph? Bipartite graph is special graph where you can divide the vertices into two sets, such that no two vertices of the same set are connected. This is why, it's possible to color a bipartite graph by using just two colors. Technically a graph is ... Read More

Advertisements