Skip to content

Commit

Permalink
add more functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ItaloCobains committed Nov 14, 2023
1 parent 1095ff7 commit 0ff1cd7
Show file tree
Hide file tree
Showing 7 changed files with 370 additions and 22 deletions.
138 changes: 138 additions & 0 deletions __test__/graph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,142 @@ describe('Graph Library', () => {
test('should return empty array for nodes of empty graph', () => {
expect(graph.getNodes()).toEqual([]);
});

test('should return empty array for neighbors of non-existent nodes', () => {
expect(graph.getNeighbors(1)).toEqual([]);
});

test('should return empty array for in neighbors of non-existent nodes', () => {
expect(graph.getInNeighbors(1)).toEqual([]);
});

test('should return empty array for out neighbors of non-existent nodes', () => {
expect(graph.getOutNeighbors(1)).toEqual([]);
});

test('should return 0 for degree of non-existent nodes', () => {
expect(graph.getDegree(1)).toEqual(0);
});

test('should return 0 for in degree of non-existent nodes', () => {
expect(graph.getInDegree(1)).toEqual(0);
});

test('should return 0 for out degree of non-existent nodes', () => {
expect(graph.getOutDegree(1)).toEqual(0);
});

test('should return true for empty graph', () => {
expect(graph.isEmpty()).toBe(true);
});

test('should return false for non-empty graph', () => {
graph.addNode(1);

expect(graph.isEmpty()).toBe(false);
});

test('should return 0 for empty graph', () => {
expect(graph.getNumNodes()).toEqual(0);
expect(graph.getNumEdges()).toEqual(0);
});

test('should return correct number of nodes and edges', () => {
graph.addNode(1);
graph.addNode(2);
graph.addNode(3);
graph.addEdge(1, 2);
graph.addEdge(2, 3);

expect(graph.getNumNodes()).toEqual(3);
expect(graph.getNumEdges()).toEqual(2);
});

test('should clear graph', () => {
graph.addNode(1);
graph.addNode(2);
graph.addEdge(1, 2);

graph.clear();

expect(graph.hasNode(1)).toBe(false);
expect(graph.hasNode(2)).toBe(false);
expect(graph.hasEdge(1, 2)).toBe(false);
});

test('should return correct degree', () => {
graph.addNode(1);
graph.addNode(2);
graph.addNode(3);
graph.addEdge(1, 2);
graph.addEdge(2, 3);

expect(graph.getDegree(1)).toEqual(1);
expect(graph.getDegree(2)).toEqual(2);
expect(graph.getDegree(3)).toEqual(1);
});

test('should return correct in degree', () => {
graph.addNode(1);
graph.addNode(2);
graph.addNode(3);
graph.addEdge(1, 2);
graph.addEdge(2, 3);

expect(graph.getInDegree(1)).toEqual(0);
expect(graph.getInDegree(2)).toEqual(1);
expect(graph.getInDegree(3)).toEqual(1);
});

test('should return correct out degree', () => {
graph.addNode(1);
graph.addNode(2);
graph.addNode(3);
graph.addEdge(1, 2);
graph.addEdge(2, 3);

expect(graph.getOutDegree(1)).toEqual(1);
expect(graph.getOutDegree(2)).toEqual(1);
expect(graph.getOutDegree(3)).toEqual(0);
});

test('should return 0 for non-existent edge', () => {
expect(graph.getEdge(1, 2)).toEqual(null);
});

test('should return correct degree', () => {
graph.addNode(1);
graph.addNode(2);
graph.addNode(3);
graph.addEdge(1, 2);
graph.addEdge(2, 3);

expect(graph.getDegree(1)).toEqual(1);
expect(graph.getDegree(2)).toEqual(2);
expect(graph.getDegree(3)).toEqual(1);
});

test('should return correct in degree', () => {
graph.addNode(1);
graph.addNode(2);
graph.addNode(3);
graph.addEdge(1, 2);
graph.addEdge(2, 3);

expect(graph.getInDegree(1)).toEqual(0);
expect(graph.getInDegree(2)).toEqual(1);
expect(graph.getInDegree(3)).toEqual(1);
});

test('should return correct out degree', () => {
graph.addNode(1);
graph.addNode(2);
graph.addNode(3);
graph.addEdge(1, 2);
graph.addEdge(2, 3);

expect(graph.getOutDegree(1)).toEqual(1);
expect(graph.getOutDegree(2)).toEqual(1);
expect(graph.getOutDegree(3)).toEqual(0);
});
});
9 changes: 9 additions & 0 deletions graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ export interface IGraph {
getInNeighbors(node: number): number[];
removeNode(node: number): void;
removeEdge(node1: number, node2: number): void;
getOutNeighbors(node: number): number[];
getEdge(node1: number, node2: number): number;
getDegree(node: number): number;
getInDegree(node: number): number;
getOutDegree(node: number): number;
isEmpty(): boolean;
clear(): void;
getNumNodes(): number;
getNumEdges(): number;
}

export var Graph: {
Expand Down
85 changes: 85 additions & 0 deletions src/graph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,88 @@ void Graph::removeNode(int nodeId)
}
}
}

std::vector<int> Graph::getOutNeighbors(int nodeId) const
{
std::vector<int> neighbors;
for (auto edge : edges)
{
if (edge.sourceId == nodeId)
{
neighbors.push_back(edge.targetId);
}
}
return neighbors;
}

Edge *Graph::getEdge(int sourceId, int targetId) const
{
for (auto edge : edges)
{
if (edge.sourceId == sourceId && edge.targetId == targetId)
{
return &edge;
}
}
return nullptr;
}

int Graph::getDegree(int nodeId) const
{
int degree = 0;
for (auto edge : edges)
{
if (edge.sourceId == nodeId || edge.targetId == nodeId)
{
++degree;
}
}
return degree;
}

int Graph::getInDegree(int nodeId) const
{
int degree = 0;
for (auto edge : edges)
{
if (edge.targetId == nodeId)
{
++degree;
}
}
return degree;
}

int Graph::getOutDegree(int nodeId) const
{
int degree = 0;
for (auto edge : edges)
{
if (edge.sourceId == nodeId)
{
++degree;
}
}
return degree;
}

bool Graph::isEmpty() const
{
return nodes.empty();
}

void Graph::clear()
{
nodes.clear();
edges.clear();
}

int Graph::getNumNodes() const
{
return nodes.size();
}

int Graph::getNumEdges() const
{
return edges.size();
}
9 changes: 9 additions & 0 deletions src/graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ class Graph
std::vector<int> getNodes() const;
std::string toString() const;
std::vector<int> getInNeighbors(int nodeId) const;
std::vector<int> getOutNeighbors(int nodeId) const;
Edge *getEdge(int sourceId, int targetId) const;
int getDegree(int nodeId) const;
int getInDegree(int nodeId) const;
int getOutDegree(int nodeId) const;
bool isEmpty() const;
void clear();
int getNumNodes() const;
int getNumEdges() const;
};

#endif
90 changes: 90 additions & 0 deletions src/wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ Napi::Object GraphWrapper::Init(Napi::Env env, Napi::Object exports)
InstanceMethod("getInNeighbors", &GraphWrapper::getInNeighbors),
InstanceMethod("removeNode", &GraphWrapper::removeNode),
InstanceMethod("removeEdge", &GraphWrapper::removeEdge),
InstanceMethod("getOutNeighbors", &GraphWrapper::getOutNeighbors),
InstanceMethod("getEdge", &GraphWrapper::getEdge),
InstanceMethod("getDegree", &GraphWrapper::getDegree),
InstanceMethod("getInDegree", &GraphWrapper::getInDegree),
InstanceMethod("getOutDegree", &GraphWrapper::getOutDegree),
InstanceMethod("isEmpty", &GraphWrapper::isEmpty),
InstanceMethod("clear", &GraphWrapper::clear),
InstanceMethod("getNumNodes", &GraphWrapper::getNumNodes),
InstanceMethod("getNumEdges", &GraphWrapper::getNumEdges),
});

constructor = Napi::Persistent(func);
Expand Down Expand Up @@ -128,4 +137,85 @@ Napi::Value GraphWrapper::removeEdge(const Napi::CallbackInfo &info)
int to = info[1].ToNumber().Int32Value();
this->graph->removeEdge(from, to);
return env.Null();
}

Napi::Value GraphWrapper::getOutNeighbors(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
int nodeId = info[0].ToNumber().Int32Value();
std::vector<int> neighboard = this->graph->getOutNeighbors(nodeId);
Napi::Array result = Napi::Array::New(env, neighboard.size());
for (int i = 0; i < neighboard.size(); i++)
{
result[i] = neighboard[i];
}
return result;
}

Napi::Value GraphWrapper::getEdge(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
int from = info[0].ToNumber().Int32Value();
int to = info[1].ToNumber().Int32Value();
Edge *edge = this->graph->getEdge(from, to);
if (edge == nullptr)
{
return env.Null();
}
Napi::Object result = Napi::Object::New(env);
result.Set("sourceId", edge->sourceId);
result.Set("targetId", edge->targetId);
return result;
}

Napi::Value GraphWrapper::getDegree(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
int nodeId = info[0].ToNumber().Int32Value();
int degree = this->graph->getDegree(nodeId);
return Napi::Number::New(env, degree);
}

Napi::Value GraphWrapper::getInDegree(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
int nodeId = info[0].ToNumber().Int32Value();
int degree = this->graph->getInDegree(nodeId);
return Napi::Number::New(env, degree);
}

Napi::Value GraphWrapper::getOutDegree(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
int nodeId = info[0].ToNumber().Int32Value();
int degree = this->graph->getOutDegree(nodeId);
return Napi::Number::New(env, degree);
}

Napi::Value GraphWrapper::isEmpty(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
bool isEmpty = this->graph->isEmpty();
return Napi::Boolean::New(env, isEmpty);
}

Napi::Value GraphWrapper::clear(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
this->graph->clear();
return env.Null();
}

Napi::Value GraphWrapper::getNumNodes(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
int numNodes = this->graph->getNumNodes();
return Napi::Number::New(env, numNodes);
}

Napi::Value GraphWrapper::getNumEdges(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
int numEdges = this->graph->getNumEdges();
return Napi::Number::New(env, numEdges);
}
9 changes: 9 additions & 0 deletions src/wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ class GraphWrapper : public Napi::ObjectWrap<GraphWrapper>
Napi::Value getInNeighbors(const Napi::CallbackInfo &info);
Napi::Value removeNode(const Napi::CallbackInfo &info);
Napi::Value removeEdge(const Napi::CallbackInfo &info);
Napi::Value getOutNeighbors(const Napi::CallbackInfo &info);
Napi::Value getEdge(const Napi::CallbackInfo &info);
Napi::Value getDegree(const Napi::CallbackInfo &info);
Napi::Value getInDegree(const Napi::CallbackInfo &info);
Napi::Value getOutDegree(const Napi::CallbackInfo &info);
Napi::Value isEmpty(const Napi::CallbackInfo &info);
Napi::Value clear(const Napi::CallbackInfo &info);
Napi::Value getNumNodes(const Napi::CallbackInfo &info);
Napi::Value getNumEdges(const Napi::CallbackInfo &info);
Graph *graph;
};

Expand Down
Loading

0 comments on commit 0ff1cd7

Please sign in to comment.