# Adjacency List Implementation in C#

Hello people..! This is a special extension for my discussion on Graph Theory Basics. Here, I give you the code for implementing the Adjacency List in C# using the .Net Library. Some of the features of this code are –

• The Adjacency List is an array of LinkedList<>, where each element is a Tuple<>. This Tuple stores two values, the destination vertex, (V2 in an edge V1 → V2) and the weight of the edge.
• For adding an edge, we can call –
• void addEdgeAtEnd(int startVertex, int endVertex, int weight) – To append an edge to the linked list.
• void addEdgeAtBegin(int startVertex, int endVertex, int weight) – To add an edge at the beginning of the linked list.
• Methods have been provided to return the number of vertices.
• An Indexer has been provided to help in retrieving the edges from a vertex. We can use Count property on this, to get the number of edges from a vertex.
• bool removeEdge(int startVertex, int endVertex, int weight) – Tries to remove the first occurrence an edge from the adjacency list. Returns true if successfull.
```using System;
using System.Collections.Generic;

namespace TheoryOfProgramming
{
{

// Constructor - creates an empty Adjacency List
{

for (int i = 0; i < adjacencyList.Length; ++i)
{
}
}

// Appends a new Edge to the linked list
public void addEdgeAtEnd(int startVertex, int endVertex, int weight)
{
}

// Adds a new Edge to the linked list from the front
public void addEdgeAtBegin(int startVertex, int endVertex, int weight)
{
}

// Returns number of vertices
// Does not change for an object
public int getNumberOfVertices()
{
}

// Returns a copy of the Linked List of outward edges from a vertex
{
get
{

return edgeList;
}
}

{
int i = 0;

{
Console.Write("adjacencyList[" + i + "] -> ");

foreach (Tuple<int, int> edge in list)
{
Console.Write(edge.Item1 + "(" + edge.Item2 + ")");
}

++i;
Console.WriteLine();
}
}

// Removes the first occurence of an edge and returns true
// if there was any change in the collection, else false
public bool removeEdge(int startVertex, int endVertex, int weight)
{
Tuple<int, int> edge = new Tuple<int, int>(endVertex, weight);

}
}

class TestGraph
{
public static void Main()
{
Console.WriteLine("Enter the number of vertices -");

Console.WriteLine("Enter the number of edges -");

Console.WriteLine("Enter the edges with weights -");
int startVertex, endVertex, weight;

for (int i = 0; i < edges; ++i)
{

}

}
}
}
```

One sweet thing about this code is that we can easily store more information in an edge. We can do so by adding elements to the generic Tuple<>… We need to give each input in a single line, otherwise an exception is thrown. If not we must split the input and then parse the array elements. This is an example demonstrates it –

```class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter -> startVertex endVertex weight");

// Spilt by space as delimeter

int startVertex = Int32.Parse(input);
int endVertex = Int32.Parse(input);
int weight = Int32.Parse(input);

Console.WriteLine(startVertex);
Console.WriteLine(endVertex);
Console.WriteLine(weight);

}
}
```

Feel free to comment if you have any doubts..! Commenting is super easy if you are a Facebook, Twitter or a Google+ user…! So, don’t hesitate..! 😉 … Keep practising..! Happy Coding..! 😀