A graph is a data structure that consists of a set of nodes connected by edges. Graphs are used to simulate many real-world problems, such as routes in cities, circuit networks, and social networks. This is graph implementation part 2 – weighted graph as an adjacency list.

graph data structure

Table of Content


Terminology

A node in the graph is also called a vertex.  A line between two nodes is an edge. Two nodes are adjacent, called neighbors if they are connected through an edge. A path represents a sequence of edges between the two nodes.

In a directed graph, all of the edges represent a one-way relationship. In an undirected graph, all edges are bidirectional. 

If the edges in the graph have weights, the graph is said to be a weighted graph. If the edges do not have weights, the graph is said to be unweighted.

The weight of the edges might represent the distances between two cities, or the cost of flights, etc. The problems such as finding the shortest path or longest path are applied to weighted graphs.

Weighted graphs can be directed or undirected. For example, the minimum spanning tree is an undirected graph. The Dijkstra’s algorithm to find the shortest path is for directed graphs.

A graph can be presented as an adjacency list or adjacency matrix. An adjacency list is a “list of lists”, i.e. a list of nodes, with each node having a list of neighbors. An adjacency list is used for the representation of a sparse graph. An adjacency matrix is a square matrix with dimensions equivalent to the number of nodes in the graph. An adjacency matrix is preferred when the graph is dense.

weighted directed graph and adjacency list

weighted directed graph and adjacency list


Map of graph implementations

Part 1 – Graph implementation as adjacency list

you are here

Part 2 – Weighted graph as adjacency list
Part 3 – Depth first search in an adjacency matrix
Part 4 – Shortest path in an adjacency matrix


Define classes

First, we define an Edge class. It has two fields: neighbor and weight. The neighbor is the node at the other end of the edge. weight is the value associated with the edge.

The GraphWeighted class has two fields: adj and directed. adj is a HashMap with nodes as keys and linked lists of edges as values. directed is a boolean variable to specify whether the graph is directed or undirected. By default, it is undirected.

The nodes can be any data type, such as Integer or String, or a customer-defined graphNode class.

Java

Javascript

Python


Add node and edge

To add a node to the graph is to add a key in the hashmap. To add an edge is to add an item in this key’s value. The following method addEdge includes both adding a node and adding an edge. For a directed graph, we add an edge from a to b. For an undirected graph, we also add an edge from b to a. Note the weight is one of the inputs to create the edge object.

Java

Javascript

Python


Remove node and edge

The removal operation can be removing an edge or removing a node. Before we continue, let’s create a utility method to find the edge between two nodes. Use one node as a key to find its neighbors. Then loop through the neighbors to find the other node. Return the first matched edge. This method will be used in the following operations.

To remove an edge, we use the node as the key to find its neighbors in the hashmap. Then remove the other node from its neighbors. For a directed graph, we need to remove the edge from a to b. For an undirected graph, we also need to remove the edge from b to a.

To remove a node, we have to remove all connected edges before removing the node itself. For an undirected graph, first, we get all the neighbors of the node. Then for each of its neighbors, remove itself from the value list. For a directed graph, we search all keys in the hashmap for their values and check whether this node exists in their neighbors. If it does, remove it. The last step is to remove the node as the key in the hashmap. Then this node is no longer in the hashmap’s key set.

Java

Javascript

Python

Search can be a search of a node, an edge, or a path. We can check whether there is a node existing in the graph. This can be done by simply checking the hashmap contains the key. We can also check whether there is a direct connection between two nodes (aka whether there is an edge). This can be done by checking whether the second node is in the first node’s neighbors.

Java

Javascript

Python


Find path with depth first search in weighted graph

A more useful operation is to search for a path. A path is a sequence of edges. There can be more than one path between two nodes. There are two common approaches: depth-first search (DFS) and breadth-first search (BFS). In this section, we use DFS and BFS to find out whether there is a path from one node to another.

Depth First Search starts from the source node and explores the adjacent nodes as far as possible before returning. DFS is usually implemented with recursion or stack. It is used to solve find-path or detect cycle problems.

Java

Javascript

Python


Find path with breadth first search in weighted graph

Breath First Search starts from the source node and explores all its adjacent nodes before going to the next level of adjacent nodes. BFS is usually implemented with a queue. It is often used to solve shortest-path problems.

Java

Javascript

Python


Print weighted graph as adjacency list

The print operation is to display all nodes and their connections in the graph. This method is used for debugging purposes. This can be done by looping through the key set of the hashmap and printing the entries in the neighbor list.

Java

JavaScript

Python


Traverse with depth first search in weighted graph

DFS traversal is to use depth-first search to visit all nodes in the graph and print the node’s information. This is similar to DFS traversal in a binary tree. Starting from the source node, we call the recursive method to visit one of its neighbors, and then a neighbor of the neighbor, and so on. Please note the source node might be any node in the graph. Some nodes might not be reached in a directed graph. (In a binary tree, we always start from the root and all nodes should be visited.)

Java

JavaScript

Python


Traverse with breadth first search in weighted graph

BFS traversal is to use breadth-first search to visit all nodes in the graph and print the node’s information. This is similar to BFS traversal in a binary tree. Starting from the source node, visit all its neighbors first before visiting the next level of neighbors. Please note the source node might be any node in the graph. Some nodes might not be reached in a directed graph. (In a binary tree, we always start from the root and all nodes should be visited.)

Java

JavaScript

Python


Free download

Download weighted graph as adjacency list in Java, JavaScript and Python code
Data structures introduction PDF

What data structure should be used to implement a weighted graph as an adjacency list in Java?

undirected weighted graph

It is HashMap. Traditionally, a weighted graph is implemented as an array of linked lists. However using an array, you have to guess and declare the initial number of nodes in the graph. HashMap doesn’t require that. The code is more clean and flexible when using HashMap.