graph
Edge list vs Adjacent list vs Adjacent matrix
Time complexity comparison between different graph representation
Use cases for different representations
Edge list is usually not used because looping through neighbor of a vertex is too expensive. This makes it really appropriate for many graph algo (bfs, dfs).
Adjacent matrix is usually used for dense graph, where vertexes are seldomly added or removed.
Adjacent list is usually used for sparse graph to save space.
Adjacent list representation is the most commonly used graph representation in an interview setting. There are two common ways to realize this. One typical classical way is to define class GraphNode and then graph can be defined as List < GraphNode >. The other way is to define graph as Map<Integer, Set<Integer>> graph. Map<Integer>
Build graph
it is will be less error-prone to separate the phase of building vertexes and edges. When they are merged together, it is easy to forget about the isolated vertexes. In a common setting, usually asked to build a graph given the number of vertex int n and an array of edges.
Detect cycles inside undirected graph
Detect cycles inside directed graph
332.Reconstruct-Itinerary (H) 753.Cracking-the-Safe (H) 1059.All-Paths-from-Source-Lead-to-Destination (H) 1192.Critical-Connections-in-a-Network (H) 1334.Find-the-City-With-the-Smallest-Number-of-Neighbors-at-a-Threshold-Distance (TBD) 1361.Validate-Binary-Tree-Nodes (TBD) 1719.Number-Of-Ways-To-Reconstruct-A-Tree (H+) 1761.Minimum-Degree-of-a-Connected-Trio-in-a-Graph (M+) 1782.Count-Pairs-Of-Nodes (H) 1820.Maximum-Number-of-Accepted-Invitations (H)
Last updated