Programming Articles - Page 2680 of 3366

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

287 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

829 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

Java program to get the difference between two time zones by seconds

Samual Sam
Updated on 16-Sep-2024 23:25:40

1K+ Views

In this article, we will learn to get the difference between two time zones by seconds using Java. We'll use LocalDateTime, ZoneId, and ZonedDateTime classes from java.time package to achieve this.Problem Statement Write a program in Java to get the difference between two time zones by seconds − Output Difference between two time zones in seconds = 28800 Steps to get the difference between two time zones by seconds Following are the steps to get the difference between two time zones by seconds − First, we will import the necessary classes LocalDateTime, ZoneId, and ZonedDateTime from ... Read More

Java Program to create custom DateTime formatter

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

218 Views

To create custom DateTime formatter, use DateTimeFormatter. Let us first see for Time −DateTimeFormatter dtFormat = new DateTimeFormatterBuilder() .appendValue(ChronoField.HOUR_OF_DAY) .appendLiteral(":") .appendValue(ChronoField.MINUTE_OF_HOUR) .appendLiteral(":") .appendValue(ChronoField.SECOND_OF_MINUTE) .toFormatter();For Date −dtFormat = new DateTimeFormatterBuilder() .appendValue(ChronoField.YEAR) .appendLiteral("/") .appendValue(ChronoField.MONTH_OF_YEAR) .appendLiteral("/") .appendValue(ChronoField.DAY_OF_MONTH) .toFormatter();Exampleimport java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; import java.time.temporal.ChronoField; public class Demo {    public static void main(String[] args) {       DateTimeFormatter dtFormat = new DateTimeFormatterBuilder()       .appendValue(ChronoField.HOUR_OF_DAY)       .appendLiteral(":")       .appendValue(ChronoField.MINUTE_OF_HOUR)       .appendLiteral(":")       .appendValue(ChronoField.SECOND_OF_MINUTE)       .toFormatter();       System.out.println("Time = "+dtFormat.format(LocalDateTime.now()));       dtFormat = new DateTimeFormatterBuilder()     ... Read More

C++ Program to Find the Transitive Closure of a Given Graph G

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

267 Views

If a directed graph is given, determine if a vertex j is reachable from another vertex i for all vertex pairs (i, j) in the given graph. Reachable mean that there is a path from vertex i to j. This reach-ability matrix is called transitive closure of a graph. Warshall algorithm is commonly used to find the Transitive Closure of a Given Graph G. Here is a C++ program to implement this algorithm.AlgorithmBegin    1. Take maximum number of nodes as input.    2. For Label the nodes as a, b, c…..    3. To check if there any edge ... Read More

Java Program to convert java.util.Date to java.time.LocalDateTime

Samual Sam
Updated on 30-Jul-2019 22:30:25

189 Views

First, set the date −java.util.Date date = new Date();Now, convert the above Date to java.time.LocalDateTime −java.time.LocalDateTime dateTime =    java.time.LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());Exampleimport java.time.ZoneId; import java.util.Date; public class Demo {    public static void main(String[] args) {       java.util.Date date = new Date();       System.out.println("Date = "+date);       java.time.LocalDateTime dateTime =          java.time.LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());       System.out.println("LocalDateTime = "+dateTime);    } }OutputDate = Thu Apr 18 23:39:34 IST 2019 LocalDateTime = 2019-04-18T23:39:34.400

Java program to convert java.util.Date to Instant

karthikeya Boyini
Updated on 09-Sep-2024 01:19:32

1K+ Views

In this article, we will learn to convert a date object to an Instant object in Java. The Instant class represents a specific moment on the timeline, often used for precise time calculations. We'll use the toInstant() method provided by the Date class to achieve this conversion Problem Statement Write a program in Java to convert date to instant. Input >Date = Thu Apr 18 23:32:07 IST 2019 Output java.util.Date to Instant = 2019-04-18T18:02:07.330Z Steps to convert java.util.Date to Instant Following are the steps to convert date to instant − Start by importing the Instant class ... Read More

Advertisements