Typical Applications of Graph:
Graph ADT
Definition
Some theorem involving degree of vertices and number of edges; a simple graph with n vertices has at most n(n-1) edges.
Graph Operations
Concrete data structures to implement a graph
Graph Traversal
Algorithm DFS(graph G, start_node v) For each edge e in G.incidentEdges(v) do If e is not marked then w = G.opposite(v, e); If w is not marked then Mark w; Mark e as discovery edge; DFS(G, w); Else mark e as back edge;
Algorithm BFS(graph G, start_node v) Create an empty queue Q; Mark v; Q.enqueue(v); While not Q.isEmpty() w = Q.dequeue(); For each edge e in G.incidentEdges(w) do If e is not marked then u = G.opposite(w, e); If u is not marked then Mark u; Mark e as discovery edge; Q.enqueue(u); Else mark e as back edge;
Other typical graph algorithms
Algorithem TopologicalSort(Graph G) Create an empty sequence S; repeat until G is empty select a vertex whose in-degree is zero; append this vertex to S; remove this vertex and all the edges eminating from it from G;
// Each node will have a label // (flag:visited_flag, d:distance_to_source, p:previous_node) // weight(x, y) = +inf if there isn't an edge from x to y Algorithm Dijkstra(Graph G, start_node v1, destination_node v2) set all nodes' label as (false, +inf, -); set current node w as the start_node v1; set current node's label as (false, 0, -); while current node w is not the destination node v2 set w.flag to be true; for each unvisited node x (x's flag is false) if (w.d + weight(w,x) < x.d) set x's label as (false, w.d+weight(w,x), w); find a node y that is unvisited and has the smallest y.d; set current node w to be node y;
Algorithm DAG(Graph G, start_node v_s) compute a topological sort sequence S for G; // S = {v1, v2, ..., vn} D[v_s] = 0; for each node v different from v_s D[v] = +inf; for i = 1 to n-1 do for each edge <v_i, u> in G if (D[v_i] + weight(v_i, u) < D[u]) set D[u] = D[v_i] + weight(v_i, u);
// G has n nodes // weight(i, j) stores the weight of the edge from node i to node j // weight(i, j) is +inf if there is no edge from node i to node j // weight(i, i) is 0 Algorithm allPairs(Graph G) for i = 0 to n-1 for j = 0 to n-1 if weight(i, j) != +inf D[i][j] = weight(i, j); next[i][j] = j; else D[i][j] = +inf; next[i][j] = null; for k = 0 to n-1 for i = 0 to n-1 for j = 0 to n-1 if (D[i][j] > D[i][k] + D[k][j]) D[i][j] = D[i][k] + D[k][j]; next[i][j] = next[i][k];
// G has n nodes // weight(i, j) stores the weight of the edge from node i to node j // weight(i, j) is +inf if there is no edge from node i to node j // weight(i, i) is 0 Algorithm allPairs(Graph G) for i = 0 to n-1 for j = 0 to n-1 A[0, i, j] = weight(i, j); for k = 1 to n-1 for i = 0 to n-1 for j = 0 to n-1 A[k, i, j] = A[k-1, i, j] for m = 0 to n-1 if A[k, i, j] > A[k-1, i, m] + weight(m, j) A[k, i, j] = A[k-1, i, m] + weight(m, j)
// Graph G has n vertices Algorithm MinimumSpanningTree(Graph G) create an empty tree T; for each vertex v in G define an elementary cluster C(v) = {v}; create an empty priority Q; for each edge e in G, using e's weight as key Q.insert(e's weight, e); while T has fewer than n-1 edges <u, v> = Q.removeMin(); if u and v are not in the same cluster then add the edge <u, v> to T; merge u's and v's cluster to one; return tree T;
// Graph G has n vertices Algorithm MinimumSpanningTree(Graph G) pick any vertex v of G; D[v] = 0; for each vertex u <> v D[u] = +inf; initialize an empty tree T; using D[u] as priority to create a priority queue Q that includes an entry of (D[u], (u, null)) for each vertex u in G; while not Q.isEmpty() (u, e) = Q.removeMin(); // e can be null add vertex u and edge e to T; for each vertex z adjacent to u and z is not in T if weight(u, z) < D[z] then D[z] = weight(u, z); change the entry of z in Q to (D[z], (z, (u, z)); return tree T;