Graph is a very useful data structure which has versatile applications in many fields. It is impossible to cover all the aspects of graph-related algorithm, (the same as all the other topics in the posts), we will focus on some basics, especially that may be useful in code interview.
The two basic components of a graph is vertex (or node) and edge. Vertices are connected by edges. Graph can be directed graph or un-directed graph. The directed graph has directed edges; the undirected has undirected edges.
The common representation of a graph can be adjacent matrix or list. If use adjacent matrix, the element is usually set as 0 or 1. 0 means there is no edge, and 1 mean connected. If use adjacent list, usually the index of the list is the source node, and the elements in the list is the connected nodes from the source.
Other important concepts are the indegrees and outdegrees. The indegree of a node means the number of edges pointing to it; the outdegree of a node means the number of edges originating from it. These two concepts are very useful in topologic sort: when the nodes has dependencies.
Besides to the common breadth-first-search (bfs), depth-first-searh (dfs), union-find (uf), there are many graph algorithm named after someone's name, such as Dijkstra algorithm. In general, the algorithm named after someone's name are designed only for some specific questions (Dijkstra however is an outlier), the chance to see them in tech interview is not very high.
To sum up,
1. graph is a useful data structure having many applications;
2. the edge could have directions;
3. the nodes could have dependencies;
4. the common basic algorithms about graph are dfs, bfs, and uf;
5. there are many algorithms about graph named after someone's' name(s), which is good to know.
Comments
Post a Comment