DS&A in java
P378
Graph representation
1. adjacency list which represents all vertices adjacent to each vertex
using linked list or star representation
2.adjacency matrix
3. incidence matrix
Note: 1 is suitable for find vertices next to v
2 and 3 are good for node insertion and deletion
最短路径算法
Dijkstra
Floyd
label-correcting algorithms
spanning tree
Kruskal's algorithm
最 大 流 算 法
Struct arc{
node * start, *end;
};
Struct node{
vector<arc *> outgoing;
};
DFS(Node v) {
Create a Set<Node> of visited nodesCreate a Stack<Node> of nodes to visit;
Add v to the stack;
while (The stack is not empty) {
Pop a node from the stack, let it be u;
if u is in the visited set
continue
for (Node w connected to u)
Push w onto the stack;
}
}
Problems with DFS
1 .Useful when trying to explore everything.
2. Not good at finding specific nodes
BFS(Node v, Set<Node> visited) {
Create a Queue<Node> of nodes to visit;
Add v to the queue;
while (The queue is not empty) {
Dequeue a node from the queue, let it be u;
if (u has been visited) continue;
Add u to the visited set;
for (Node w connected to u)
Enqueue w in the queue;
}
}
Word ladder
Classic Graph Algorithms
graph coloring