Skip to content

This project implements a basic directed graph using HashMap and explores it using Depth-First Search (DFS).

Notifications You must be signed in to change notification settings

Muchangi001/dfs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation


Graph Traversal in Rust (DFS)

This project implements a basic directed graph using HashMap and explores it using Depth-First Search (DFS).


Features

  • Graph represented as an adjacency list (HashMap<String, Vec<String>>)
  • DFS traversal with cycle handling using a HashSet<String>
  • Simple, readable code suitable for learning or quick prototyping

Structure

struct Graph {
    graph: HashMap<String, Vec<String>>,
    visited: HashSet<String>,
}
  • graph: Adjacency list storing directed edges
  • visited: Keeps track of visited nodes to avoid infinite recursion in cyclic graphs

Example Graph

   A
  / \
 B   C
 |    \
 D     E

Edges are added manually to simulate a graph with bidirectional links:

g.add_edge("A", "B");
g.add_edge("A", "C");
g.add_edge("B", "A");
g.add_edge("B", "D");
g.add_edge("C", "A");
g.add_edge("C", "E");
g.add_edge("D", "B");
g.add_edge("E", "C");

How It Works

DFS Traversal

fn dfs(&mut self, start: &str)
  • Starts at a node
  • Prints visited node
  • Recursively visits unvisited neighbors

Output

Visited -> A
Visited -> B
Visited -> D
Visited -> C
Visited -> E

DFS dives deep along one branch before backtracking. The visited set prevents revisiting nodes in cycles.


How to Run

  1. Save the code to a file like main.rs
  2. Run with:
cargo run

About

This project implements a basic directed graph using HashMap and explores it using Depth-First Search (DFS).

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages