I am confused as to what, exactly, an adjacency list is and what a priority queue is. - Java

StackOverflow https://stackoverflow.com/questions/19941965

  •  30-07-2022
  •  | 
  •  

Question

I'm a bit confused as to what an adjacency list is and what a priority queue is.

I am going to make my adjacency list with an Arraylist. What I'm confused about is what is held in the adjacency list.

Is the adjacency list used to show what is being pointed at?

Example, You have the following data:

u v weight
1 4   6
2 5   7
3 7   1
1 3   2
1 2   5
2 2   9

So for the adjacency list, would it look something like this:

1 --->4---->3---->2
2 --->5---->2
3 --->7

Where each "u" that is the same, (i.e. there are three 1's under the u column) points to the corresponding "v"

Was it helpful?

Solution

An adjacency list of a node in a graph gives you all the nodes that are neighbors of that node. Basically an adjacency list is a node's way of saying "I can get to these other nodes starting from myself" or "Here are the nodes that I am connected to". A more concrete example is if you imagine a city (let's call it Graphville) that has roads leading to other cities next to it. Then Graphville is the node, and a list of all cities you can get to directly (i.e., not passing through other cities) from Graphville would be in Graphville's adjacency list.

A priority queue is a data structure that is like a regular queue, except that each element has a "priority" associated with it. Typically, higher-priority elements are processed before lower-priority elements in a priority queue.

OTHER TIPS

In graph theory and computer science, an adjacency list representation of a graph is a collection of unordered lists, one for each vertex in the graph. Each list describes the set of neighbors of its vertex.

Means it just stores the neighbors of a node. So we can store the neighbors in linkedList or Array or ArrayList.

priority queue is an abstract data type which is like a regular queue or stack data structure, but where additionally each element has a "priority" associated with it. In a priority queue, an element with high priority is served before an element with low priority. If two elements have the same priority, they are served according to their order in the queue.

In java you have PriorityQueue, PriorityBlockingQueue.

From wiki

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top