CS106B Lecture 23 Graph

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值