Laboratory 3 - Task 1 #
Starting code #
Graphs
Introduction to Graphs #
A graph is a data structure that consists of a set of vertices and a set of edges that connect pairs of vertices. Graphs are used to model relationships between objects and are widely applied in various domains, such as social networks, mapping and navigation systems, computer networks, and more.
There are several ways to represent a graph in memory. The two most common representations are:
Adjacency Matrix: A 2D array where the entry at
matrix[i][j]
is1
ortrue
if there is an edge from vertexi
to vertexj
, and0
orfalse
otherwise. This representation is efficient for checking if an edge exists between two vertices but can consume a lot of memory for sparse graphs (graphs with few edges).Adjacency List: An array of lists, where the list at index
i
contains all the vertices adjacent to vertexi
. This representation is more memory-efficient for sparse graphs, as it only stores the existing edges.
In this laboratory, you will implement both of these representations.
General Requirements #
The goal of this laboratory is to implement a directed graph data structure and depth-first search algorithm associated with it. All classes should be in the Graphs
namespace.
Stage 1: Graph Representation (6 points) #
This stage focuses on creating the basic graph structure and two different implementations for representing the graph’s edges.
1.1. Abstract
Graph
Class (2 points)- Define an abstract class
Graph
. - It should have a protected constructor
Graph(int vertexCount)
that initializes a public, read-onlyint VertexCount
property. - Declare the following abstract methods:
- public
List<int> GetOutVertices(int vertex);
- returns neighbors list ofvertex
- public
void AddEdge(int from, int to);
- add directed edge to the graph
- public
- Implement a public
void Display()
method that prints the graph’s adjacency information to the console. The output for each vertex should be in the formatvertex -> [neighbor1,neighbor2,...]
, for example:0 -> [1,2] 1 -> [3] 2 -> [] 3 -> [1]
- Define an abstract class
1.2. Adjacency List Implementation (2 points)
- Create a public class
AdjacencyListGraph
, which inherits fromGraph
. - It should have a public
AdjacencyListGraph(int vertexCount)
constructor. - Implement the
GetOutVertices
andAddEdge
methods using aList<int>[]
to store the graph structure.
- Create a public class
1.3. Adjacency Matrix Implementation (2 points)
- Create a public class
AdjacencyMatrixGraph
, which inherits fromGraph
. - It should have a public
AdjacencyMatrixGraph(int vertexCount)
constructor. - Implement the
GetOutVertices
andAddEdge
methods using abool[,]
to store the graph structure.
- Create a public class
Stage 2: Graph Traversal and Pathfinding (6 points) #
This stage involves adding graph traversal logic using the Depth-First Search (DFS) algorithm and implementing the visitor pattern to process vertices during traversal.
Depth-First Search (DFS) is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root node (or an arbitrary node in a graph) and explores as far as possible along each branch before backtracking. It is usually implemented using recursion.
2.1. Depth-First Search (DFS) Algorithm (3 points)
- Add a public
void DepthFirstSearch(int start, IVertexVisitor visitor)
method to the abstractGraph
class. - This method should implement the DFS traversal algorithm recursively.
- The traversal should start from the
start
vertex. - Before the recursive call for each neighbor, the method should call
PreVisit
from theIVertexVisitor
interface. - After the recursive call for each neighbor, the method should call
PostVisit
from theIVertexVisitor
interface. - The method might use a helper method, for example
DepthFirstSearchRecursive(int vertex, bool[] visited, IVertexVisitor visitor)
.- Use a
bool[] visited
array to mark and skip already visited vertices.
- Use a
- Add a public
2.2.
IVertexVisitor
Interface- Define a public interface
IVertexVisitor
. - This interface should declare the following methods:
void PreVisit(int vertex);
void PostVisit(int vertex);
- Define a public interface
2.3.
PathFinderVisitor
(3 points)- Create a public class
PathFinderVisitor
that implements theIVertexVisitor
interface. - It should have a public constructor:
PathFinderVisitor(int target)
. - It should have the following properties:
public List<int> Path { get; }
- A list of vertices forming the found path.public bool Found { get; private set; }
- A flag indicating whether the path has been found.
- Implement the
PreVisit
andPostVisit
methods to find a path to thetarget
vertex. - Override the
ToString()
method to return the found path in the formatv1 --> v2 --> ...
or a “not found” message.
- Create a public class
Example solution #
Graphs