From 61812932ac2eee40a79317682a72eae9bdb6d467 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Mon, 27 May 2024 06:24:26 +0200 Subject: [PATCH] Initial --- Makefile | 11 ++++---- README.md | 3 +++ src/main.c | 1 + src/main.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 README.md create mode 100644 src/main.cpp diff --git a/Makefile b/Makefile index 91a48c6..34c2db3 100644 --- a/Makefile +++ b/Makefile @@ -1,24 +1,25 @@ BIN := graphs # Compiler -CC := gcc +# CC := gcc +CC := g++ GITHASH := $(shell git rev-parse --short HEAD) # Compiler flags -CFLAGS := -Wall -Wextra -Wpedantic -std=c2x +CFLAGS := -Wall -Wextra -Wpedantic -std=c++23 # Directories SRC_DIR := src BUILD_DIR := build # Source files -SRCS := $(wildcard $(SRC_DIR)/*.c) +SRCS := $(wildcard $(SRC_DIR)/*.cpp) # Header files (used for formatting) HEADERS := $(wildcard $(SRC_DIR)/*.h) # Object files -OBJS := $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/%.o,$(SRCS)) +OBJS := $(patsubst $(SRC_DIR)/%.cpp,$(BUILD_DIR)/%.o,$(SRCS)) # Target executable TARGET := $(BUILD_DIR)/$(BIN) @@ -31,7 +32,7 @@ $(TARGET): $(OBJS) $(CC) $(CFLAGS) $^ -o $@ # Rule to build object files -$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c +$(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp @mkdir -p $(BUILD_DIR) $(CC) $(CFLAGS) -c $< -o $@ diff --git a/README.md b/README.md new file mode 100644 index 0000000..8c3bfe0 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Graphs + +Practice in writing different graph algorithms in C/C++ diff --git a/src/main.c b/src/main.c index cda6155..312cd93 100644 --- a/src/main.c +++ b/src/main.c @@ -50,6 +50,7 @@ struct Graph* createGraph(int vertices) { struct Graph* graph = malloc(sizeof(struct Graph)); graph->numVertices = vertices; + // Allocates a list with n*the size of a pointer, essentially graph->adjLists = malloc(vertices * sizeof(struct node*)); graph->visited = malloc(vertices * sizeof(int)); diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..11dfb7f --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,73 @@ +// DFS algorithm in C++ + +#include +#include +using namespace std; + +// Interface for Expr +class Expr { + public: + virtual void print() = 0; +}; + +class BinaryExpr : public Expr { + public: + BinaryExpr(Expr* lhs, Expr* rhs) : lhs(lhs), rhs(rhs) {} + + void print() { + lhs->print(); + rhs->print(); + } + + private: + Expr* lhs; + Expr* rhs; +}; + +class Graph { + int numVertices; + // Adjlist is a list of lists + list* adjLists; + bool* visited; + + public: + Graph(int V); + void addEdge(int src, int dest); + void DFS(int vertex); +}; + +// Initialize graph +Graph::Graph(int vertices) { + numVertices = vertices; + // Note that this is creating 'vertices' number of lists + adjLists = new list[vertices]; + visited = new bool[vertices]; +} + +// Add edges +void Graph::addEdge(int src, int dest) { adjLists[src].push_front(dest); } + +// DFS algorithm +void Graph::DFS(int vertex) { + visited[vertex] = true; + // Get the list for this particular vertex + list adjList = adjLists[vertex]; + + cout << vertex << " "; + + list::iterator i; + for (i = adjList.begin(); i != adjList.end(); ++i) + if (!visited[*i]) DFS(*i); +} + +int main() { + Graph g(4); + g.addEdge(0, 1); + g.addEdge(0, 2); + g.addEdge(1, 2); + g.addEdge(2, 3); + + g.DFS(2); + + return 0; +}