Theory
A graph is a non-linear data structure consisting of vertices and edges. Graphs can represent relationships between objects such as cities and roads, users and friendships or web pages and links.
Example
const graph = {
A: ["B", "C"],
B: ["A", "D"],
C: ["A"],
D: ["B"]
};
console.log(graph.A);
Expected Output
The neighbors of A are B and C.
Practice
Create a graph representing four cities and their connections.
Interview Questions
- What is a graph?
- What are vertices and edges?
- What is a directed graph?
- What is an undirected graph?