CS106B Lecture 23 Graph

来源:互联网 发布:循环冗余检查 数据丢失 编辑:程序博客网 时间:2024/04/28 05:33

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

Bellman–Ford algorithm

label-correcting algorithms

Floyd–Warshall algorithm


spanning tree

Kruskal's algorithm




Struct arc{

node * start, *end;

};


Struct node{

vector<arc *> outgoing;

};


DFS(Node v) {

     Create a Set<Node> of visited nodes
     Create 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

原创粉丝点击