图的遍历

图的遍历

standford algorithm learning notes

广度优先

Breadth First Search(BFS)
To systematically explore the nodes of the graph beginning with the given starting vertex in layers.

Algorithm

BFS(graph G, start vertex s)
[all nodes initially unexplored]

  1. mark s as explored.
  2. let Q = queue data structure (FIFO), initialized with s.
  3. while Q ϕ :
    1. remove the first node from Q, call it v.
    2. for each edge(v, w):
      if w is unexplored:
      • mark w as explored
      • add w to Q(at the end)

Basic properties

  • at the end of BFS, v exposed G has a path from s to v.
  • running time of main while loop = O( ns + ms ), where ns is number of nodes findable from s, ms is number of edges findable from s.

Application : shortest paths

Goal: Compute dist(v), the fewest number of edges on a path from s to v.
Extra code:

  • initialize dist(v) = { 0, if v = s; or 1, if v s
  • when considering edge (v, w):
    • if w unexplored, then set dist(w) = dist(v) + 1

Claim: at termination, dist(v) = i v is in i th layer.

深度优先

Depth First Search(DFS)

  • much more aggressive search than BFS where you immediately try to plunge as deep as you can only backtracking when absolutely necessary.
  • preserve topological ordering in directed graph.

Aogorithm

Recursive version: DFS(graph G, start vertex s)
1. mark s as explored.
2. for every edge(s, v):

  • if v unexplored:
    • DFS(G, v)

Basic properties

  • at the end of the algorithm, v marked as explored path from s to v in G.

    Application: topological ordering

    definition: A topological ordering of a directed graph G is a labelling F of G’s nodes such that :
    1. the f(v)’s are the set {1,2, ,n}
    2. (u, v) f(u) < f(v)

    topological sort via DFS:

    DFS-loop(graph G)
    1. mark s as explored.
    2. current_label = n[to keep track of ordering]
    3. for each vertex v G:

    if v unexplored:
    DFS(G, v)

    set f(v) = current_label
    current_label

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
void CreateGraph Graph graph 的创建 { ENode p q e; int i; cout<<"请输入连通无向的顶点数和边数 例如 3 3: n"; scanf "%d %d" &graph >numberOfVerts &graph >numberOfEerts ; for i 1;i< graph >numberOfVerts;i++ { cout<<"请输入第"<<i<<"个顶点的信息: n"; cin>>graph >amlist [i] data; graph >amlist [i] number i; graph >amlist[i] firstedge NULL; graph >amlist [i] mark 0; } for i 1;i< graph >numberOfEerts;i++ { p ENode malloc sizeof ENode ; cout<<"请输入每条边的信息(编号小的在前 例如1 3回车1 2回车2 3) n"; cin>>p >ivex>>p >jvex; p >ilink p >jlink NULL; if graph >amlist [p >ivex ] firstedge NULL graph >amlist [p >ivex ] firstedge p; else { q graph >amlist [p >ivex ] firstedge ; while q NULL { e q; if q >ivex p >ivex q q >ilink ; else q q >jlink ; } if e >ivex p >ivex e >ilink p; else e >jlink p; } if graph >amlist [p >jvex ] firstedge NULL graph >amlist [p >jvex ] firstedge p; else { q graph >amlist [p >jvex ] firstedge ; while q NULL { e q; if q >ivex p >ivex q q >ilink ; else q q >jlink ; } if e >ivex p >ivex e >ilink p; else e >jlink p; } } } void SetMark Graph graph 设置访问标记 { int i; for i 1;i< graph >numberOfVerts ;i++ graph >amlist [i] mark 0; } void DFS Graph graph int v 深度遍历 { ENode p; cout<<v<<" "; graph >amlist [v] mark 1; p graph >amlist [v] firstedge ; while p NULL { if p >ivex v { if graph >amlist [p >jvex ] mark 0 { cout<<"<"<<p >ivex<<" "<<p >jvex<<">"<<endl; DFS graph p >jvex ; } p p >ilink ; } else { if graph >amlist [p >ivex] mark 0 { cout<<"<"<<p >jvex<<" "<<p >ivex<<">"<<endl; DFS graph p >ivex ; } p p >jlink ; } } }">void CreateGraph Graph graph 的创建 { ENode p q e; int i; cout<<"请输入连通无向的顶点数和边数 例如 3 3: n"; scanf "%d %d" &graph >numberOfVerts &graph >numberOfEerts ; for i 1;i< graph >numberOfVerts;i++ { cou [更多]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值