Java GraphIn Java, the Graph is a data structure that stores a certain of data. The concept of the graph has been stolen from the mathematics that fulfills the need of the computer science field. It represents a network that connects multiple points to each other. In this section, we will learn Java Graph data structure in detail. Also, we will learn the types of Graph, their implementation, and traversal over the graph. GraphA graph is a data structure that stores connected data. In other words, a graph G (or g) is defined as a set of vertices (V) and edges (E) that connects vertices. The examples of graph are a social media network, computer network, Google Maps, etc. Each graph consists of edges and vertices (also called nodes). Each vertex and edge have a relation. Where vertex represents the data and edge represents the relation between them. Vertex is denoted by a circle with a label on them. Edges are denoted by a line that connects nodes (vertices). Graph TerminologyVertex: Vertices are the point that joints edges. It represents the data. It is also known as a node. It is denoted by a circle and it must be labeled. To construct a graph there must be at least a node. For example, house, bus stop, etc. Edge: An edge is a line that connects two vertices. It represents the relation between the vertices. Edges are denoted by a line. For example, a path to the bus stop from your house. Weight: It is labeled to edge. For example, the distance between two cities is 100 km, then the distance is called weight for the edge. Path: The path is a way to reach a destination from the initial point in a sequence. Types of Graph
Java Graph ImplementationFor the implementation of graphs in Java we will use the Generic class. To create an object of Java Generic class, we use the following syntax: Remember that, we cannot use primitive type for parameter type. Let's create a Java program that implements Graph. GraphImplementation.java Output: Implementation of Directed GraphDirectedGraph.java Output: Implementation of Weighted GraphWeightedGraph.java Output: Graph TraversalTraversal over the graph means visit each and every vertex and edge at least once. To traverse over the graph, Graph data structure provides two algorithms:
Depth-First Search (DFS)DFS algorithm is a recursive algorithm that is based on the backtracking concept. The algorithm starts from the initial node and searches in depth until it finds the goal node (a node that has no child). Backtracking allows us to move in the backward direction on the same path from which we have traversed in the forward direction. Let's implement the DFS algorithm in a Java program. DepthFirstSearch.java Output: Breadth First Search (BFS)BFS algorithm is the most common approach to traverse over the graph. The traversal starts from the source node and scans its neighboring nodes (child of the current node). In short, traverse horizontally and visit all the nodes of the current layer. After that, move to the next layer and perform the same. Let's implement the BFS algorithm in a Java program. BreadthFirstSearch.java Output: |