-
Notifications
You must be signed in to change notification settings - Fork 0
Graph
krishanthan4 edited this page Dec 16, 2025
·
1 revision
Implement a Graph data structure that can represent relationships between vertices (nodes) using an adjacency list representation.
A graph is a non-linear data structure consisting of vertices (nodes) and edges (connections between vertices). Graphs can be directed or undirected, weighted or unweighted.
-
Time Complexity:
- Add vertex: O(1)
- Add edge: O(1)
- Remove edge: O(E) where E is number of edges from vertex
- Check if edge exists: O(E)
- Space Complexity: O(V + E) where V is vertices and E is edges
- Directed: Edges have direction (A → B)
- Undirected: Edges are bidirectional (A ↔ B)
- Weighted: Edges have weights/costs
- Unweighted: All edges have same weight
Adjacency List (recommended):
Map<Integer, List<Edge>>
Vertex 1 → [2, 3, 4]
Vertex 2 → [1, 5]
Vertex 3 → [1]
Edge Structure:
- destination vertex
- weight (optional)
Implement the following methods in Graph.java:
-
addVertex(int vertex)- Add a vertex -
addEdge(int source, int dest, int weight)- Add weighted edge -
addEdge(int source, int dest)- Add unweighted edge -
removeEdge(int source, int dest)- Remove an edge -
getNeighbors(int vertex)- Get all adjacent vertices -
hasEdge(int source, int dest)- Check if edge exists -
getAllVertices()- Get all vertices
- Add vertices and edges
- Directed vs undirected behavior
- Weighted edges
- Remove edges
- Check edge existence
- Get neighbors of a vertex
- Handle non-existent vertices
- Self-loops (vertex to itself)
- Multiple edges between same vertices
- Use
HashMap<Integer, List<Edge>>for adjacency list - For undirected graphs: add edges in both directions
- Consider creating an Edge class to store destination and weight
- Check if vertices exist before adding edges
- Handle null cases gracefully
Directed Graph:
addEdge(1, 2); // Only 1 → 2Undirected Graph:
addEdge(1, 2); // Both 1 → 2 and 2 → 1- Traversal: BFS, DFS
- Shortest Path: Dijkstra, Bellman-Ford, Floyd-Warshall
- Minimum Spanning Tree: Kruskal's, Prim's
- Cycle Detection: DFS-based
- Topological Sort: For DAGs
- Connected Components: Union-Find
- Social networks (friends, followers)
- Maps and navigation (roads, cities)
- Computer networks (routers, connections)
- Dependency graphs (tasks, prerequisites)
- Web page links
- Recommendation systems
- How would you detect a cycle in your graph?
- How would you find the shortest path between two vertices?
- How would you determine if the graph is connected?
- How would you implement graph coloring?
- What's the difference between adjacency list and adjacency matrix?
| Aspect | Adjacency List | Adjacency Matrix |
|---|---|---|
| Space | O(V + E) | O(V²) |
| Add Edge | O(1) | O(1) |
| Remove Edge | O(E) | O(1) |
| Check Edge | O(E) | O(1) |
| Best For | Sparse graphs | Dense graphs |
- Not handling directed/undirected correctly
- Forgetting to check if vertices exist
- Memory issues with dense graphs (use matrix instead)
- Not removing edges from both directions in undirected graphs
- Adjacency List: Map of vertex to list of neighbors (implemented here)
- Adjacency Matrix: 2D array of edges
- Edge List: List of all edges
- Incidence Matrix: Matrix of vertices vs edges
- Depth First Search (DFS)
- Breadth First Search (BFS)
- Dijkstra's Algorithm
- Topological Sort
- Union-Find
- Minimum Spanning Tree
- Number of Islands
- Course Schedule (topological sort)
- Word Ladder
- Network Delay Time
- Clone Graph
- Alien Dictionary