This is how I do it. I’m still a novice so it might not be very efficient but it works. Please correct me if I’m wrong anywhere and suggestions for improving my methods will be extremely helpful and appreciated

**1. How to represent graph in java in adjacency list ?**

**One way:**

Let `u`

be a node in a graph G and say `{v1,v2,v3...vn}`

be its neighbours. So `u`

must be linked to all its neighbours.

`u-->{v1,v2,v3...vn}`

.

To make the link we can use a `Map<K,V>`

which will hold every individual vertex as its **keys** and the **value** corresponding to **each key** will be a list that will contain all the neighbouring vertices of the present **key**/node `u`

. So we can make an ArrayList to hold all the neighbouring vertices `{v1,v2,v3...vn}`

and then we will make the current vertex `u`

point towards this list *(making the link*) in our map. So adjacency list has been made for every node and declaration can be done in the following way.

```
HashMap<Integer,ArrayList<Integer>> my_map = new HashMap<Integer,ArrayList<Integer>>();
```

This is when all the nodes of the graph are integers,change the type according to your needs.

**Another way:**

Representation can also be done in this way. Its like an array of lists.

```
List<Integer> graph[];
```

The i’th index of the array (graph[]) holds a list which contains all the nodes in the graph that the i’th node is connected to. If there are say N nodes,initialize the graph with empty lists.

```
for(int i = 0; i < N; i++) {
graph[i] = new ArrayList<Integer>();
}
```

**2. Searching a node in a graph.**

This can be done by bfs/dfs. Which one to use can be determined by the depth of the graph. In short,if depth is very large,dfs is preferable otherwise bfs will be better. Here is a very neat code that will help you to understand how it is being done. If you don’t prefer to make new node class for the purpose as shown in the link,in that case*(it will get lengthy)* create a method like `int get_unvisited_childnodes_of_node(int root_node)`

which will traverse through the adjacency list of `root_node`

and will return its unvisited child nodes/neighbours.

## Suggested problems

I haven’t learnt max flow yet so can’t say anything about that. Its an advanced topic so before working on maxflow make sure that you know the basic graph algorithms well.

Good luck!