Program For Adjacency List

← Program for Merge Sort in C Depth First Search (DFS) Program in C → 4 thoughts on “ Representation of Graphs: Adjacency Matrix and Adjacency List ”.

Graph is a data structure that consists of following two components: 1. A finite set of vertices also called as nodes. A finite set of ordered pair of the form (u, v) called as edge. The pair is ordered because (u, v) is not same as (v, u) in case of directed graph(di-graph). The pair of form (u, v) indicates that there is an edge from vertex u to vertex v. The edges may contain weight/value/cost.

Graphs are used to represent many real life applications: Graphs are used to represent networks. The networks may include paths in a city or telephone network or circuit network. Graphs are also used in social networks like linkedIn, facebook. For example, in facebook, each person is represented with a vertex(or node). Each node is a structure and contains information like person id, name, gender and locale. See for more applications of graph.

Adjacency List Representation Of Graph

Following is an example undirected graph with 5 vertices. Following two are the most commonly used representations of graph. Adjacency Matrix 2. Adjacency List There are other representations also like, Incidence Matrix and Incidence List. The choice of the graph representation is situation specific. It totally depends on the type of operations to be performed and ease of use. Adjacency Matrix: Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in a graph.

Adjacency list java code

Let the 2D array be adj, a slot adjij = 1 indicates that there is an edge from vertex i to vertex j. Adjacency matrix for undirected graph is always symmetric. Hp deskjet 6840 vista.

Adjacency Matrix Vs Adjacency List

Adjacency Matrix is also used to represent weighted graphs. If adjij = w, then there is an edge from vertex i to vertex j with weight w. The adjacency matrix for the above example graph is. Adjacency Matrix Representation of the above graph Pros: Representation is easier to implement and follow. Removing an edge takes O(1) time. Queries like whether there is an edge from vertex ‘u’ to vertex ‘v’ are efficient and can be done O(1).

Cons: Consumes more space O(V^2). Even if the graph is sparse(contains less number of edges), it consumes the same space.

Adding a vertex is O(V^2) time. Adjacency List: An array of linked lists is used. Size of the array is equal to number of vertices.

Let the array be array. An entry arrayi represents the linked list of vertices adjacent to the ith vertex. This representation can also be used to represent a weighted graph. The weights of edges can be stored in nodes of linked lists. Following is adjacency list representation of the above graph.

Here is a simple implementation for creating adjacency list.According to the problem: There will be n linked lists each of variable size. First Initialize an ArrayList of linked lists of integers: ArrayList adjlist = new ArrayList; Then simply add linked lists by repeating following code: adjlist.add(new LinkedList); If you are using it to represent graph,then no of linked lists=no of vertices.So you must repeat the above line n(no of vertices) times.

Say now you want to add numbers 3,4,5 to your first linked lists.Do the following: adjlist.get(0).add(3); adjlist.get(0).add(4); adjlist.get(0).add(5); It simply means there is an edge in your graph from vertex 0 to 3,4,5. First your description seems to be of an adjacency matrix except you're saying m by n. Adjacency matrices are always square, so we must assume mn. The matrix elements are the edge weights. An adjacency list representation of a graph is (usually) an array adj of sets of pairs. The set adji contains pair iff there is a directed edge i-w-j, i.e. From vertex i to j with weight w in the represented graph.

With this definition, it's clear you must start with n empty adjacency sets adji and then iterate over the matrix elements mij = w. For each of these add to adji.

The java code for this is pretty trivial, so I won't write it. The type for a graph represented with adjacency lists is something like ArrayList adjacencies. The pairs in adji are mappings j - w stored in the hash table adjacencies.get(i).

The code to create such an adjacency will be adjacencies.get(i).put(j, w). This method allows you to iterate over the vertices adjacent to i by iterating over keys in the hash table adjacencies.get(i), look up the weight of a given edge i-w-j with w = adjacencies.get(i).get(j), and so on for all the usual graph operations.