This language bar is your friend. Select your favorite languages!

Idiom #40 Graph with adjacency lists

Declare a Graph data structure in which each Vertex has a collection of its neighbouring vertices. As an example, set G to a representation of the "bull graph":

struct Vertex
	float x, y, z;
	Vertex* [] adjacentVertices;
type Vertex struct{
	Id int
	Label string
	Neighbours map[*Vertex]bool

type Graph []*Vertex
datatype Node = Int
datatype Adjacencies = [ Node ]
datatype Graph = [ Adjacencies ]
import java.util.List;
class Graph{
  List<Vertex> vertices;

  static class Vertex{
    int id;
    List<Vertex> neighbours;
import java.util.Set;
class Graph{
  Set<Vertex> vertices;

  static class Vertex{
    int id;
    Set<Vertex> neighbours;
from collections import defaultdict
class Vertex(set): pass
class Graph(defaultdict):
  def __init__(self, *paths):
    self.default_factory = Vertex
    for path in paths:

  def make_path(self, labels):
    for l1, l2 in zip(labels, labels[1:]):

G = Graph((0, 1, 2, 3), (1, 4, 2))
Vertex =, :y, :z)
Graph =, :neighbours)

v =, 2, 3)
neighbours = [, 3, 4),, 5, 6) ]
graph =, neighbours)

Do you know the best way to do this in your language ?
New implementation...