graph.prog
资源名称:leda.tar.gz [点击查看]
上传用户:gzelex
上传日期:2007-01-07
资源大小:707k
文件大小:11k
源码类别:
数值算法/人工智能
开发平台:
MultiPlatform
- {bf Depth First Search}
- #include <LEDA/graph.h>\
- #include <LEDA/stack.h>
- list<node> DFS($graph&$ $G$, $node$ $v$, $node_array<bool>&$ $reached$)
- ${$
- hspace*{.5cm}list<node> $L$;\
- hspace*{.5cm}stack<node> $S$;\
- hspace*{.5cm}node $w$;
- smallskip
- hspace*{.5cm}If ( !$reached[v]$ )\
- hspace*{.70cm}${$ $reached[v]$ = true;\
- hspace*{1cm}$S$.push($v$);\
- hspace*{.70cm} $}$
- smallskip
- hspace*{.5cm}{bf while} ( !$S$.empty() )\
- hspace*{1.5cm}${$ $v = S$.pop();\
- hspace*{1.85cm}$L$.append($v$);\
- hspace*{1.85cm}{bf forall_adj_nodes}($w,v$)\
- hspace*{2.25cm}{bf if} ( !$reached[w]$ )\
- hspace*{2.5cm}${$ $reached[w]$ = true;\
- hspace*{2.75cm}$S$.push($w$);\
- hspace*{2.5cm}$}$
- smallskip
- hspace*{1.5cm}$}$
- smallskip
- hspace{.5cm}return $L$;
- smallskip
- $}$
- bigskip
- bigskip
- {bf Breadth First Search}
- bigskip
- #include <LEDA/graph.h>\
- #include <LEDA/queue.h>
- void BFS($graph& G, node v, node_array<int>& dist$)
- ${$
- hspace*{.5cm}queue<node> $Q$;\
- hspace*{.5cm}node $w$;
- smallskip
- hspace*{.5cm}{bf forall_nodes}($w,G$) $dist[w] = -1$;
- smallskip
- hspace*{.5cm}$dist[v] = 0$;\
- hspace*{.5cm}$Q$.append($v$);
- smallskip
- hspace*{.5cm}{bf while} ( !$Q$.empty() )\
- hspace*{1.5cm}${$ $v = Q$.pop();\
- hspace*{1.75cm}{bf forall_adj_nodes}($w,v$)\
- hspace*{2.25cm}{bf if} ($dist[w] < 0$)\
- hspace*{2.5cm}${$ $Q$.append($w$);\
- hspace*{2.75cm}$dist[w] = dist[v]+1$;\
- hspace*{2.5cm}$}$\
- hspace*{1.5cm}$}$
- smallskip
- $}$
- bigskip
- bigskip
- bigskip
- {bf Connected Components}
- bigskip
- #include <LEDA/graph.h>
- int COMPONENTS($ugraph&$ $G$, $node_array<int>&$ $compnum$)
- ${$
- hspace*{.5cm}node $v,w$;\
- hspace*{.5cm}list<node> $S$;\
- hspace*{.5cm}int $count = 0$;
- smallskip
- hspace*{.5cm}node_array(bool) $reached(G,false)$;
- smallskip
- hspace*{.5cm}Forallnodes($v,G$)
- hspace*{1cm}If ( !$reached[v]$ )\
- hspace*{1.25cm}${$ $S$ = DFS($G,v,reached$);\
- hspace*{1.5cm}Forall($w,S$) $compnum[w] = count$;\
- hspace*{1.5cm}$count++$;\
- hspace*{1.25cm}$}$
- smallskip
- hspace*{.5cm}Return $count$;\
- $}$
- newpage
- {bf Depth First Search Numbering}
- bigskip
- #include <LEDA/graph.h>
- int $dfs_count1, dfs_count2$;
- void
- parbox[t]{14cm}{d_f_s($node$ $v$,$node_array<bool>&$ $S$,
- $node_array<int>&$ $dfsnum$,
- $node_array<int>&$ $compnum$,
- $list<edge>$ $T$ )}
- ${$ // recursive DFS
- smallskip
- hspace*{.5cm}node $w$;\
- hspace*{.5cm}edge $e$;
- smallskip
- hspace*{.5cm}$S[v] = true$;\
- hspace*{.5cm}$dfsnum[v] = ++dfs_count1$;
- smallskip
- hspace*{.5cm}Foralladjedges($e,v$)\
- hspace*{1cm}${$ $w = G$.target(e);\
- hspace*{1.25cm}If ( !$S[w]$ ) \
- hspace*{1.5cm}${$ $T$.append($e$);\
- hspace*{1.75cm}d_f_s($w,S,dfsnum,compnum,T$);\
- hspace*{1.5cm}$}$\
- hspace*{1cm}$}$
- smallskip
- hspace*{.5cm}$compnum[v] = ++dfs_count2$;\
- $}$
- bigskip
- bigskip
- list<edge> DFS_NUM($graph& G, node_array<int>& dfsnum, node_array<int>& compnum$ )
- ${$ \
- hspace*{.5cm}list<edge> $T$;\
- hspace*{.5cm}node_array<bool> $reached(G,false)$;\
- hspace*{.5cm}node $v$;\
- hspace*{.5cm}$dfs_count1 = dfs_count2 = 0$;\
- hspace*{.5cm}Forallnodes($v,G$) \
- hspace*{1cm}If ( !$reached[v]$ ) d_f_s($v,reached,dfsnum,compnum,T$);\
- hspace*{.5cm}Return $T$;\
- $}$\
- newpage
- {bf Topological Sorting}
- #include <LEDA/graph.h>
- bool TOPSORT($graph& G, node_array<int>& ord$)
- ${$\
- hspace*{.5cm}node_array<int> INDEG($G$);\
- hspace*{.5cm}list<node> ZEROINDEG;
- smallskip
- hspace*{.5cm}int $count=0$;\
- hspace*{.5cm}node $v,w$;\
- hspace*{.5cm}edge $e$;
- smallskip
- hspace*{.5cm}{bf forall_nodes}($v,G$)\
- hspace*{1cm}{bf if} ((INDEG[$v$]=$G$.indeg($v$))==0) ZEROINDEG.append($v$);
- smallskip
- hspace*{.5cm}{bf while} (!ZEROINDEG.empty())\
- hspace*{1cm}${$ $v$ = ZEROINDEG.pop();\
- hspace*{1.25cm}$ord[v] = ++count$;
- smallskip
- hspace*{1.25cm}{bf forall_adj_nodes}($w,v$) \
- hspace*{1.75cm}{bf if} ($--$INDEG[$w$]==0) ZEROINDEG.append($w$);\
- hspace*{1cm} $}$
- smallskip
- hspace*{.5cm}{bf return} (count==G.number_of_nodes());
- smallskip
- $}$\
- bigskip
- //TOPSORT1 sorts node and edge lists according to the topological ordering:
- bigskip
- $bool$ TOPSORT1($graph& G$)
- smallskip
- {${$\
- hspace*{.5cm}node_array<int> node_ord($G$);\
- hspace*{.5cm}edge_array<int> edge_ord($G$);
- smallskip
- hspace*{.5cm}{bf if} (TOPSORT(G,node_ord))\
- hspace*{.75cm}${$ edge $e$;\
- hspace*{1cm}{bf forall_edges}($e,G$) edge_ord[$e$]=node_ord[$target(e)$];\
- hspace*{1cm}$G$.sort_nodes(node_ord);\
- hspace*{1cm}$G$.sort_edges(edge_ord);\
- hspace*{1cm}{bf return} true;\
- hspace*{.75cm}$}$\
- hspace*{.5cm}{bf return} false;\
- $}$\
- newpage
- {bf Strongly Connected Components}
- #include <LEDA/graph.h>\
- #include <LEDA/array.h>
- medskip
- int STRONG_COMPONENTS($graph& G, node_array<int>& compnum$)\
- ${$\
- hspace*{.5cm}node $v,w$;\
- hspace*{.5cm}int $n = G$.number_of_nodes();\
- hspace*{.5cm}int $count = 0$;\
- hspace*{.5cm}int $i$;
- smallskip
- hspace*{.5cm}array<node> $V(1,n)$;\
- hspace*{.5cm}list<node> $S$;\
- hspace*{.5cm}node_array<int> $dfs_num(G), compl_num(G)$;\
- hspace*{.5cm}node_array<bool> $reached(G,false)$;
- smallskip
- hspace*{.5cm}DFS_NUM($G,dfs_num,compl_num$);
- smallskip
- hspace*{.5cm}Forallnodes($v,G$) $V[compl_num[v]] = v$;
- smallskip
- hspace*{.5cm}$G$.rev();
- smallskip
- hspace*{.5cm}For($i=n; i>0; i--$)\
- hspace*{1cm}If ( !$reached[V[i]]$ ) \
- hspace*{1.25cm}${$ $S$ = DFS($G,V[i],reached$);\
- hspace*{1.5cm}Forall($w,S$) $compnum[w] = count$;\
- hspace*{1.5cm}$count++$;\
- hspace*{1.25cm}$}$
- smallskip
- hspace*{.5cm}Return $count$;
- smallskip
- $}$\
- newpage
- {bf Dijkstra's Algorithm}
- #include <LEDA/graph.h>\
- #include <LEDA/node_pq.h>
- medskip
- void DIJKSTRA( graph& $G$, node $s$, edge_array<int>& $cost$, \
- hspace*{1cm}node_array<int>& $dist$, node_array<edge>& $pred$ )
- ${$\
- hspace*{.5cm}node_pq<int> $PQ(G)$;
- smallskip
- hspace*{.5cm}int $c$;\
- hspace*{.5cm}node $u,v$;\
- hspace*{.5cm}edge $e$;
- smallskip
- hspace*{.5cm}{bf forall_nodes}($v,G$)\
- hspace*{1cm}${$ $ pred[v] = 0$;\
- hspace*{1.25cm}$dist[v] = infinity$;\
- hspace*{1.25cm}$PQ$.insert($v,dist[v])$;\
- hspace*{1cm}$}$
- smallskip
- hspace*{.5cm}$dist[s] = 0$;\
- hspace*{.5cm}$PQ$.decrease_inf($s,0)$;
- smallskip
- hspace*{.5cm}{bf while} ( ! $PQ$.empty())\
- hspace*{1cm}${$ $u = PQ$.del_min();
- smallskip
- hspace*{1.25cm}{bf forall_adj_edges}($e,u$)\
- hspace*{1.75cm}${$ $v = G.target(e) $;\
- hspace*{2cm}$c = dist[u] + cost[e] $;\
- hspace*{2cm}{bf if} ( $c < dist[v] $)\
- hspace*{2.25cm}${$ $dist[v] = c$;\
- hspace*{2.5cm}$pred[v] = e$;\
- hspace*{2.5cm}$PQ$.decrease_inf($v,c$);\
- hspace*{2.25cm}$}$\
- hspace*{1.75cm}$}$ /$*$ forall_adj_edges $*$
- smallskip
- hspace*{1cm}$}$ /$*$ while $*$/
- smallskip
- $}$\
- newpage
- {bf Bellman/Ford Algorithm}
- #include <LEDA/graph.h>\
- #include <LEDA/queue.h>
- medskip
- bool BELLMAN_FORD($graph& G, node s, edge_array<int>& cost$,\
- hspace*{1cm}$node_array<int>& dist, node_array<edge>& pred$)
- ${$\
- hspace*{.5cm}node_array<bool> $in_Q(G,false)$;\
- hspace*{.5cm}node_array<int> $count(G,0)$;
- smallskip
- hspace*{.5cm}int $n = G$.number_of_nodes();\
- hspace*{.5cm}queue<node> $Q(n)$;
- smallskip
- hspace*{.5cm}node $u,v$;\
- hspace*{.5cm}edge $e$;\
- hspace*{.5cm}int $c$;
- smallskip
- hspace*{.5cm}Forallnodes($v,G$)\
- hspace*{1cm}${$ $pred[v] = 0$;\
- hspace*{1.25cm}$dist[v] = infinity$; \
- hspace*{1cm}$}$\
- hspace*{.5cm}$dist[s] = 0$;\
- hspace*{.5cm}$Q$.append($s$);\
- hspace*{.5cm}$in_Q[s] = true$;
- smallskip
- hspace*{.5cm}{bf while} (!$Q$.empty())\
- hspace*{1cm}${$ $u = Q$.pop();\
- hspace*{1.25cm}$in_Q[u] = false$;
- smallskip
- hspace*{1.25cm}If ($++count[u] > n$) {bf return} false;quad //negative cycle
- smallskip
- hspace*{1.25cm}Foralladjedges($e,u$) \
- hspace*{1.75cm}${$ $v$ = $G$.target($e$);\
- hspace*{2cm}$c = dist[u] + cost[e]$;
- smallskip
- hspace*{2cm}If ($c < dist[v]$) \
- hspace*{2.25cm}${$ $dist[v] = c$; \
- hspace*{2.5cm}$pred[v] = e$;\
- hspace*{2.5cm}If (!$in_Q[v]$) \
- hspace*{2.75cm}${$ $Q$.append($v$);\
- hspace*{3cm}$in_Q[v]=true$;\
- hspace*{2.75cm}$}$\
- hspace*{2.25cm}$}$\
- hspace*{1.75cm}$}$ /$*$ forall_adj_edges $*$/\
- hspace*{1cm}$}$ /$*$ while $*$/\
- hspace*{.5cm}{bf return} true;\
- $}$\
- newpage
- {bf All Pairs Shortest Paths}
- #include <LEDA/graph.h>
- void all_pairs_shortest_paths(graph& $G$, edge_array<double>& $cost$,\
- hspace*{1cm}node_matrix<double>& $DIST$)\
- ${$\
- hspace*{.5cm}// computes for every node pair $(v,w)$ $DIST(v,w)$ = cost of the least cost\
- hspace*{.5cm}// path from $v$ to $w$, the single source shortest paths algorithms BELLMAN_FORD\
- hspace*{.5cm}// and DIJKSTRA are used as subroutines
- smallskip
- hspace*{.5cm}edge $e$;\
- hspace*{.5cm}node $v$;\
- hspace*{.5cm}double $C = 0$;
- smallskip
- hspace*{.5cm}{bf forall_edges}($e,G$) $C += fabs(cost[e]$);\
- hspace*{.5cm}node $s = G$.new_node(); hspace{3cm} // add $s$ to $G$\
- hspace*{.5cm}{bf forall_nodes}($v,G$) $G$.new_edge($s,v$); hspace{.75cm} // add edges ($s,v$) to $G$
- smallskip
- hspace*{.5cm}node_array<double> $dist1(G)$;\
- hspace*{.5cm}node_array<edge> $pred(G)$;\
- hspace*{.5cm}edge_array<double> $cost1(G)$;\
- hspace*{.5cm}{bf forall_edges}($e,G$)
- $cost1[e] = (G$.source$(e)==s)$ ? $C : cost[e]$;
- smallskip
- hspace*{.5cm}BELLMAN_FORD($G,s,cost1,dist1,pred$);
- smallskip
- hspace*{.5cm}$G$.del_node($s$); hspace{4.75cm}// delete $s$ from $G$\
- hspace*{.5cm}edge_array(double) $cost2(G)$;\
- hspace*{.5cm}{bf forall_edges}($e,G$) $cost2[e] = dist1[G.source(e)] + cost[e] - dist1[G.target(e)]$;
- smallskip
- hspace*{.5cm}{bf forall_nodes}($v,G$) DIJKSTRA($G,v,cost2,DIST[v],pred$);
- smallskip
- hspace*{.5cm}{bf forall_nodes}($v,G$)\
- hspace*{1cm}bf forall_nodes}($w,G$) $DIST(v,w) = DIST(v,w) - dist1[v] + dist1[w]$;\
- $}$
- newpage
- {bf Minimum Spanning Tree}
- #include <LEDA/graph.h>\
- #include <LEDA/node_partition.h>
- medskip
- void MIN_SPANNING_TREE(graph& $G$, edge_array<double>& $cost$, list<edge>& $EL$)\
- ${$\
- hspace*{.5cm}node $v,w$;\
- hspace*{.5cm}edge $e$;\
- hspace*{.5cm}node_partition $Q(G)$;
- smallskip
- hspace*{.5cm}$G$.sort_edges($cost$);
- smallskip
- hspace*{.5cm}$EL$.clear();\
- hspace*{.5cm}{bf forall}_edges($e,G$)\
- hspace*{.75cm}${$ $v = G.source(e)$;\
- hspace*{1cm}$w = G.target(e)$;\
- hspace*{1cm}{bf if} ($!(Q$.same_block($v,w$))\
- hspace*{1.25cm}${$ $Q$.union_blocks($v,w$);\
- hspace*{1.5cm}$EL$.append($e$);\
- hspace*{1.25cm}$}$\
- hspace*{.75cm}$}$\
- $}$\
- newpage