# Building an Adjacency List: having Dictionary issues, need data structure help!

I’m teaching myself the unity framework by building a simple tile-based strategy game. The game board will be stitched together by an adjacency list.

The list itself consists of a Dictionary<int, Vertex>, correlating each vertex in the list to its unique key.
Each Vertex is built around a Dictionary<Vector3, Vertex> which stores the direction and destination of each edge attached to it.

While testing my list, I’ve been noticing some irregular behavior with the Dictionary.ContainsKey() and List.Contains() methods and their interaction with the Vector3 data type. They are inaccurately returning false for vectors with X-axis components, even if the keys are present and perfectly fetch-able in the dictionary or list.

Here’s a stripped-down Vertex class, showing the problem functions:

``````public class Vertex
{
public int key {get; private set;}
public GameTile tile {get; private set;}

private Dictionary<Vector3, Vertex> _adj = new Dictionary<Vector3, Vertex>();

public void AddEdge(Vertex v, Vector3 dir)
{
// skip if edge already exists
if(_adj.ContainsKey(dir)) // always returns false for horizontal vectors!
return;
Debug.Log ("Adding " + v.key + " to " + this.key + ", Dir = " + dir.ToString());
}

{
{
return null;
}
}
``````

With this irregular behavior in mind, I’m strongly considering switching to a different data structure for my Vertex class. Trouble is, I’m having a hard time thinking of a ready-made one that fits my needs, so I need help brainstorming a data structure that:

• Stores adjacent Vertices or their unique keys for looking up on a master list.
• Is capable of quickly fetching neighbors based on directional information.
• Directions between neighbors are regular and consistent for all tiles, i.e. hex grid and square grid.
• Is capable of removing edges (performance doesn’t matter as much for this one).
• Can be told to operate on all stored members (for shortest paths operations)

One possibility I’m thinking of is replacing the Vector3 key with a type that doesn’t cause trouble, and adding static functions for encoding and decoding Vector3s into their key. However, I’m sure there’s a better solution out there.

If you want to see my full source, here’s my AdjacencyList.cs file: