-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphRepresentation.js
More file actions
executable file
·36 lines (28 loc) · 1.09 KB
/
graphRepresentation.js
File metadata and controls
executable file
·36 lines (28 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Graph representation using adjacency list
// what is graph representation?
// Graph representation is a way of organizing and storing the information about a graph's structure, including its nodes (vertices) and the connections (edges) between them. Common methods of graph representation include adjacency lists, adjacency matrices, and edge lists.
// Mental model:
// “Representing a graph is like creating a map of cities (nodes) and the roads (edges) that connect them, allowing you to see how each city is linked to others.”
const graph = {
A: ["B", "C"],
B: ["D"],
C: ["E"],
D: [],
E: [],
};
// Example usage:
console.log(graph);
// Output:
// {
// A: ['B', 'C'],
// B: ['D'],
// C: ['E'],
// D: [],
// E: []
// }
// time complexity for adjacency list:
// - Space complexity: O(V + E) where V is the number of vertices and E is the number of edges
// - Checking if an edge exists: O(V) in the worst case
// - Iterating through all neighbors of a vertex: O(k) where k is the number of neighbors
// edge cases
console.log(graph["F"]); // Output: undefined (node F does not exist)