Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[202410080126] - Adding Graph BFS #97

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion apps/bismarck-web/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,20 @@
"projectType": "application",
"tags": [],
"// targets": "to see all targets run: nx show project bismarck-web --web",
"targets": {}
"targets": {
"lint:fix": {
"executor": "@nx/eslint:lint",
"options": {
"fix": true
}
},
"test": {
"executor": "@nx/jest:jest",
"options": {
"jestConfig": "apps/bismarck-web/jest.config.ts",
"passWithNoTests": true,
"watch": true
}
}
}
}
11 changes: 0 additions & 11 deletions libs/data-structure/README.md

This file was deleted.

19 changes: 0 additions & 19 deletions libs/data-structure/project.json

This file was deleted.

9 changes: 0 additions & 9 deletions libs/data-structure/src/lib/Graph/Node.ts

This file was deleted.

2 changes: 0 additions & 2 deletions libs/data-structure/src/lib/Graph/index.ts

This file was deleted.

9 changes: 0 additions & 9 deletions libs/data-structure/src/lib/LinkedList/LinkedList.test.ts

This file was deleted.

11 changes: 0 additions & 11 deletions libs/data-structure/src/lib/LinkedList/Node.ts

This file was deleted.

11 changes: 11 additions & 0 deletions libs/data-structures/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# data-structures

This library was generated with [Nx](https://nx.dev).

## Building

Run `nx build data-structures` to build the library.

## Running unit tests

Run `nx test data-structures` to execute the unit tests via [Jest](https://jestjs.io).
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export default {
displayName: 'data-structure',
displayName: 'data-structures',
preset: '../../jest.preset.js',
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/libs/data-structure',
coverageDirectory: '../../coverage/libs/data-structures',
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@bismarck/data-structure",
"name": "@bismarck/data-structures",
"version": "0.0.1",
"dependencies": {
"tslib": "^2.3.0"
Expand Down
33 changes: 33 additions & 0 deletions libs/data-structures/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "data-structures",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/data-structures/src",
"projectType": "library",
"tags": [],
"targets": {
"build": {
"executor": "@nx/js:tsc",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/libs/data-structures",
"main": "libs/data-structures/src/index.ts",
"tsConfig": "libs/data-structures/tsconfig.lib.json",
"assets": ["libs/data-structures/*.md"]
}
},
"lint:fix": {
"executor": "@nx/eslint:lint",
"options": {
"fix": true
}
},
"test": {
"executor": "@nx/jest:jest",
"options": {
"jestConfig": "libs/data-structures/jest.config.ts",
"passWithNoTests": true,
"watch": true
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export * from './lib/data-structure';
export * from './lib/Graph';
export * from './lib/LinkedList';
export * from './lib/Queue';
export * from './lib/Stack';
export * from './lib/Stack';
42 changes: 42 additions & 0 deletions libs/data-structures/src/lib/Graph/BFS/BFS.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { AdjacencyList } from '../Graph.types';

export function bfs(
graph: AdjacencyList,
source: number,
needle: number
): number[] {
const visited = new Set();
const queue: number[] = [source];
const previousNode = new Map<number, number>();

while (queue.length) {
const current = queue.unshift();

if (!current || current === needle) {
break;
}

visited.add(current);

const adjacencies = graph.get(current) ?? [];
for (let idx = 0; idx <= adjacencies.length; idx++) {
const vertex = adjacencies[idx];
if (visited.has(vertex)) continue;

visited.add(adjacencies[idx]);
previousNode.set(vertex, current);
}
}

if (previousNode.get(needle) === undefined) return [];

const pathToNode: number[] = [];
let current: number | undefined = needle;

while (current != undefined) {
pathToNode.push(current);
current = previousNode.get(current);
}

return pathToNode.reverse();
}
1 change: 1 addition & 0 deletions libs/data-structures/src/lib/Graph/BFS/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './BFS';
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Node } from './Node';
import { Node } from './Vertex';

export class Graph<T> {
directed: boolean;
nodes: Array<Node>;
nodes: Array<Node<T>>;

constructor(directed = false) {
this.directed = directed;
Expand All @@ -15,24 +15,24 @@ export class Graph<T> {

removeNode(value: T) {
const isNotThisNode = (nodeValue: T) => {
return (node: Node) => node.value !== nodeValue;
return (node: Node<T>) => node.value !== nodeValue;
};

// filter main node's list
this.nodes = this.nodes.filter(isNotThisNode(value));
// filter every node's edges
this.nodes.forEach((node: Node) => {
this.nodes.forEach((node: Node<T>) => {
node.edges = node.edges.filter(isNotThisNode(value));
});
}

getNode(value: T): Node | undefined {
return this.nodes.find((node: Node) => node.value === value);
getNode(value: T): Node<T> | undefined {
return this.nodes.find((node: Node<T>) => node.value === value);
}

addEdge(firstValue: T, secondValue: T) {
const firstNode: Node | undefined = this.getNode(firstValue);
const secondNode: Node | undefined = this.getNode(secondValue);
const firstNode: Node<T> | undefined = this.getNode(firstValue);
const secondNode: Node<T> | undefined = this.getNode(secondValue);

if (firstNode && secondNode) {
firstNode.edges.push(secondNode);
Expand Down
4 changes: 4 additions & 0 deletions libs/data-structures/src/lib/Graph/Graph.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type AdjacencyList = Map<number, Array<number>>;

export type AdjacencyMatrix = number[][];

19 changes: 19 additions & 0 deletions libs/data-structures/src/lib/Graph/Vertex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export class Vertex {
value: number;
edges: Vertex[];

constructor(value: number) {
this.value = value;
this.edges = [];
}
}

export class Node<T> {
value: T;
edges: Array<Node<T>>;

constructor(value: T) {
this.value = value;
this.edges = [];
}
}
2 changes: 2 additions & 0 deletions libs/data-structures/src/lib/Graph/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './Graph';
export * from './Vertex';
9 changes: 9 additions & 0 deletions libs/data-structures/src/lib/LinkedList/LinkedList.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { LinkedList } from './LinkedList';
describe('LinkedList', () => {
it('initalize empty list', () => {
const list = new LinkedList();

expect(list).toBeInstanceOf(LinkedList);
expect(list.length).toBe(0);
});
});
11 changes: 11 additions & 0 deletions libs/data-structures/src/lib/LinkedList/Node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export class Node<T> {
data: T;
next: Node<T> | null;
previous: Node<T> | null;

constructor(data: T) {
this.data = data;
this.next = null;
this.previous = null;
}
}
File renamed without changes.
3 changes: 3 additions & 0 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
"cache": true,
"dependsOn": ["^build"],
"inputs": ["production", "^production"]
},
"@nx/eslint:lint": {
"cache": true
}
}
}
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
"name": "@bismarck/source",
"version": "0.0.0",
"license": "MIT",
"scripts": {},
"scripts": {
"lint": "nx run-many --all --target=lint",
"lint:fix": "nx run-many --all --target=lint:fix",
"test": "nx run-many --all --target=test --watch"
},
"private": true,
"dependencies": {
"react": "18.3.1",
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"skipDefaultLibCheck": true,
"baseUrl": ".",
"paths": {
"@bismarck/data-structure": ["libs/data-structure/src/index.ts"]
"@bismarck/data-structures": ["libs/data-structures/src/index.ts"]
}
},
"exclude": ["node_modules", "tmp"]
Expand Down
Loading