This commit is contained in:
Imbus 2024-05-27 06:24:26 +02:00
parent 2b329b4031
commit 61812932ac
4 changed files with 83 additions and 5 deletions

View file

@ -1,24 +1,25 @@
BIN := graphs BIN := graphs
# Compiler # Compiler
CC := gcc # CC := gcc
CC := g++
GITHASH := $(shell git rev-parse --short HEAD) GITHASH := $(shell git rev-parse --short HEAD)
# Compiler flags # Compiler flags
CFLAGS := -Wall -Wextra -Wpedantic -std=c2x CFLAGS := -Wall -Wextra -Wpedantic -std=c++23
# Directories # Directories
SRC_DIR := src SRC_DIR := src
BUILD_DIR := build BUILD_DIR := build
# Source files # Source files
SRCS := $(wildcard $(SRC_DIR)/*.c) SRCS := $(wildcard $(SRC_DIR)/*.cpp)
# Header files (used for formatting) # Header files (used for formatting)
HEADERS := $(wildcard $(SRC_DIR)/*.h) HEADERS := $(wildcard $(SRC_DIR)/*.h)
# Object files # Object files
OBJS := $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/%.o,$(SRCS)) OBJS := $(patsubst $(SRC_DIR)/%.cpp,$(BUILD_DIR)/%.o,$(SRCS))
# Target executable # Target executable
TARGET := $(BUILD_DIR)/$(BIN) TARGET := $(BUILD_DIR)/$(BIN)
@ -31,7 +32,7 @@ $(TARGET): $(OBJS)
$(CC) $(CFLAGS) $^ -o $@ $(CC) $(CFLAGS) $^ -o $@
# Rule to build object files # Rule to build object files
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c $(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp
@mkdir -p $(BUILD_DIR) @mkdir -p $(BUILD_DIR)
$(CC) $(CFLAGS) -c $< -o $@ $(CC) $(CFLAGS) -c $< -o $@

3
README.md Normal file
View file

@ -0,0 +1,3 @@
# Graphs
Practice in writing different graph algorithms in C/C++

View file

@ -50,6 +50,7 @@ struct Graph* createGraph(int vertices) {
struct Graph* graph = malloc(sizeof(struct Graph)); struct Graph* graph = malloc(sizeof(struct Graph));
graph->numVertices = vertices; graph->numVertices = vertices;
// Allocates a list with n*the size of a pointer, essentially
graph->adjLists = malloc(vertices * sizeof(struct node*)); graph->adjLists = malloc(vertices * sizeof(struct node*));
graph->visited = malloc(vertices * sizeof(int)); graph->visited = malloc(vertices * sizeof(int));

73
src/main.cpp Normal file
View file

@ -0,0 +1,73 @@
// DFS algorithm in C++
#include <iostream>
#include <list>
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<int>* 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<int>[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<int> adjList = adjLists[vertex];
cout << vertex << " ";
list<int>::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;
}