Euler tour: a cycle that uses each edge exactly once, go look around the graph and touch every edge in it.
Hamilton tour: a cycle that uses each vertex exactly once.
minimal spanning tree: the shortest set of edges, or the best way to connect all of the vertices.
Graph API
the adjacency list representation, that’s where we keep a vertex index array where, for every vertex, we maintain a list of the vertices that are adjacent to that.
Depth-First Search
Goal: Find all vertices connected to s (and a corresponding path).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//pseudo cod To visit a vertex v : - Mark vertex v as visited. - Recursively visit all unmarked vertices adjacent to v. //code private void dfs(Graph G, int v) { marked[v] = true; for (int w : G.adj(v)) if (!marked[w]) { dfs(G, w); edgeTo[w] = v; } }
Breadth-first is not a recursive algorithm, it uses a queue as a axillary data structure And it’s also quite simple to explain. So what we’re going to do is we’re going to put the source vertex on a queue and then repeat the following until the queue is empty. Remove the next vertex from the queue in order. Then add to the queue all unmarked vertices that are adjacent to these and mark them and just keep doing that until the queue is empty.
replace queue with stack ,and this algorithm can work as DFS.
// pseudo code BFS (from source vertex s) Put s onto a FIFO queue, and mark s as visited. Repeat until the queue is empty: remove the least recently added vertex v add each of v's unvisited neighbors to the queue and mark them as visited
//code private void bfs(Graph G, int s) { Queue<Integer> q = new Queue<Integer>(); q.enqueue(s); marked[s] = true; while (!q.isEmpty()) { int v = q.dequeue(); for (int w : G.adj(v)) { if (!marked[w]) { q.enqueue(w); marked[w] = true; edgeTo[w] = v; } } // end for } // end while } // end bfs
Connected Components
So recall an equivalence relation has these three properties. Every vertex is connected to itself. If v is connected to w then w is connected to v. And if v is connected to w and w to x then v is connected to x.
GOAL: Preprocess graph to answer queries of the form is v connected to w? in constant time. Depth-first search. Yes.
initialize all vertices v as unmarked. For each unmarked vertex v, run DFS to identify all vertices discovered as part of the same component.
//code private boolean[] marked; // id[v] = (index or ID of component) private int[] id; // record component index private int count;
public CC(Graph G) { marked = new boolean[G.V()]; id = new int[G.V()]; for (int v = 0; v < G.V(); v++) { if (!marked[v]) { // run DFS from one vertex // in each component dfs(G, v); count++; } // end if } // end for } // end CC
private void dfs(Graph G, int v) { marked[v] = true; // all vertices discovered in same // call of dfs have same id id[v] = count ; for (int w : G.adj(v)) if (!marked[w]) dfs(G, w); }