Skip to content

Commit

Permalink
add: adjacent list sample
Browse files Browse the repository at this point in the history
  • Loading branch information
thutasann committed Sep 1, 2024
1 parent f903be0 commit df96376
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions node-concepts/src/leetcodes/src/graph/adjacency-list/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
type AdjacencyList = {
[node in 'A' | 'B' | 'C' | 'D' | 'E' | 'F']: string[];
};

/** Example Adjacency List */
const graph: AdjacencyList = {
A: ['B', 'C'],
B: ['A', 'D', 'E'],
C: ['A', 'F'],
D: ['B'],
E: ['B', 'F'],
F: ['C', 'E'],
};

/** Add Edge */
function addEdge(graph: AdjacencyList, node1: string, node2: string) {
if (!graph[node1]) graph[node1] = [];

if (!graph[node2]) graph[node2] = [];

graph[node1].push(node2);
graph[node2].push(node1);
}

addEdge(graph, 'G', 'H');

console.log('graph -> ', graph);

0 comments on commit df96376

Please sign in to comment.