# Category Archives: Graph

## Bellman Ford Algorithm (Simple Implementation)

We have introduced Bellman Ford and discussed on implementation here. Input: Graph and a source vertex src Output: Shortest distance to all vertices from src.… Read More »

## Kth largest node among all directly connected nodes to the given node in an undirected graph

Given two arrays u and v, representing a graph such that there is an undirected edge from u[i] to v[i] (0 ≤ v[i], u[i] <… Read More »

## XOR of all the nodes in the sub-tree of the given node

Given an n-ary tree and Q queries where each query consists of an integer u which denotes a node. The task is to print the… Read More »

## Arrange array elements such that last digit of an element is equal to first digit of the next element

Given an array arr[] of integers, the task is to arrange the array elements such that the last digit of an element is equal to… Read More »

## Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing

Given a connected graph with N vertices and M edges. The task is to find the shortest path from source to the destination vertex such… Read More »

## Minimum steps to color the tree with given colors

Given a tree with N nodes which initially have no color and an array color[] of size N which represent the color of each node… Read More »

## Check if a directed graph is connected or not

Given a directed graph. The task is to check if the given graph is connected or not. Examples: Input: Output: Yes Input: Output: No Recommended:… Read More »

## Islands in a graph using BFS

Given a boolean 2D matrix, find the number of islands. A group of connected 1s forms an island. For example, the below matrix contains 5… Read More »

## Shortest paths from all vertices to a destination

Given a Weighted Directed Graph and a destination vertex in the graph, find the shortest distance from all vertex to the destination vertex. Input :… Read More »

## Graph Representation using Java ArrayList

Prerequisite : Graph and its representations In this article, we will be discussing Adjacency List representation of Graph using ArrayList in Java. Following is adjacency… Read More »

## Travelling Salesman Problem implementation using BackTracking

Travelling Salesman Problem (TSP): Given a set of cities and distance between every pair of cities, the problem is to find the shortest possible route… Read More »

## Check if the given Binary Expressions are valid

Given n expressions of the type x = y and x != y where 1 ≤ x, y ≤ n, the task is to check… Read More »

## Convert the undirected graph into directed graph such that there is no path of length greater than 1

Given an undirected graph with N vertices and M edges and no self loops or multiple edges. The task is to convert the given undirected… Read More »

## Program to count Number of connected components in an undirected graph

Given an undirected graph g, the task is to print the number of connected components in the graph. Examples: Input: Output: 3 There are three… Read More »

## Detect cycle in the graph using degrees of nodes of graph

Given a graph, the task is to detect a cycle in the graph using degrees of the nodes in the graph and print all the… Read More »