Java Program to Represent Graphs Using Linked List
Data structures are divided into two categories Linear data structures and Non-Linear data structures. The major disadvantage of the linear data structure is we cannot arrange the data of linear data structure in hierarchy manner that’s why in the computer field we use Non-Linear data structures. The most commonly used Non-Linear data structure is Graphs and Trees both data structures can be implemented using Linear data structures. In this article, we will discuss how to represent Graphs using Linked List.
Graphs consist of a finite set of vertices(or nodes) and a set of edges that connect a pair of nodes. Graphs are represented in two different ways. One method is using adjacency List representation and the second is adjacency matrix representation. Adjacency List representation is mostly used because of dynamic memory allocation using list representation.
Graphs are of two types:
- Directed Graphs: In this arrangement of graphs, every node is directed to one vertex is called directed Graphs.
- Undirected Graphs: In this arrangement of graphs, two nodes are connected using bi-direction vertex is called undirected graphs.
Undirected Graphs representation: Maximum number of a vertex in the undirected graphs is n*(n-1) where n is the total number of nodes present in the Undirected Graphs.
LinkedList Representation of undirected Graphs is as follows:
In undirected graphs, two nodes are connected in bi-direction vertex. We can use both Array List and Linked List collections to represent the undirected graphs. In Linked List the Manipulation of the data is faster than the Array List because Array List internally used the dynamic array to store the data whereas Linked List used Doubly Linked List that is faster in the operation of manipulation but not in accessing the elements.
Implementation: Here we will be discussing out both types of graphs in order to implement the same.
0->1 4 1->0 2 3 4 2->1 3 3->1 2 4 4->0 1 3
Now jumping onto the next type of graphs that is our directed graphs representation using Linked List. In directed graphs, two nodes are connected in uni-direction vertex.
0->1 4 1->2