@ -2077,7 +2077,104 @@
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
< div class = "highlight" > < span class = "filename" > graph_adjacency_matrix.cs< / span > < pre > < span > < / span > < code > < a id = "__codelineno-7-1" name = "__codelineno-7-1" href = "#__codelineno-7-1" > < / a > < span class = "na" > [class]< / span > < span class = "p" > {< / span > < span class = "n" > GraphAdjMat< / span > < span class = "p" > }< / span > < span class = "o" > -< / span > < span class = "p" > [< / span > < span class = "n" > func< / span > < span class = "p" > ]{}< / span >
< div class = "highlight" > < span class = "filename" > graph_adjacency_matrix.cs< / span > < pre > < span > < / span > < code > < a id = "__codelineno-7-1" name = "__codelineno-7-1" href = "#__codelineno-7-1" > < / a > < span class = "cm" > /* 基于邻接矩阵实现的无向图类 */< / span >
< a id = "__codelineno-7-2" name = "__codelineno-7-2" href = "#__codelineno-7-2" > < / a > < span class = "k" > class< / span > < span class = "w" > < / span > < span class = "nc" > GraphAdjMat< / span >
< a id = "__codelineno-7-3" name = "__codelineno-7-3" href = "#__codelineno-7-3" > < / a > < span class = "p" > {< / span >
< a id = "__codelineno-7-4" name = "__codelineno-7-4" href = "#__codelineno-7-4" > < / a > < span class = "w" > < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "kt" > int< / span > < span class = "o" > > < / span > < span class = "w" > < / span > < span class = "n" > vertices< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "c1" > // 顶点列表,元素代表“顶点值”,索引代表“顶点索引”< / span >
< a id = "__codelineno-7-5" name = "__codelineno-7-5" href = "#__codelineno-7-5" > < / a > < span class = "w" > < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "kt" > int< / span > < span class = "o" > > > < / span > < span class = "w" > < / span > < span class = "n" > adjMat< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "c1" > // 邻接矩阵,行列索引对应“顶点索引”< / span >
< a id = "__codelineno-7-6" name = "__codelineno-7-6" href = "#__codelineno-7-6" > < / a >
< a id = "__codelineno-7-7" name = "__codelineno-7-7" href = "#__codelineno-7-7" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 构造函数 */< / span >
< a id = "__codelineno-7-8" name = "__codelineno-7-8" href = "#__codelineno-7-8" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "nf" > GraphAdjMat< / span > < span class = "p" > (< / span > < span class = "kt" > int< / span > < span class = "p" > []< / span > < span class = "w" > < / span > < span class = "n" > vertices< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "p" > [][]< / span > < span class = "w" > < / span > < span class = "n" > edges< / span > < span class = "p" > )< / span >
< a id = "__codelineno-7-9" name = "__codelineno-7-9" href = "#__codelineno-7-9" > < / a > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-7-10" name = "__codelineno-7-10" href = "#__codelineno-7-10" > < / a > < span class = "w" > < / span > < span class = "k" > this< / span > < span class = "p" > .< / span > < span class = "n" > vertices< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "k" > new< / span > < span class = "w" > < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "kt" > int< / span > < span class = "o" > > < / span > < span class = "p" > ();< / span >
< a id = "__codelineno-7-11" name = "__codelineno-7-11" href = "#__codelineno-7-11" > < / a > < span class = "w" > < / span > < span class = "k" > this< / span > < span class = "p" > .< / span > < span class = "n" > adjMat< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "k" > new< / span > < span class = "w" > < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "kt" > int< / span > < span class = "o" > > > < / span > < span class = "p" > ();< / span >
< a id = "__codelineno-7-12" name = "__codelineno-7-12" href = "#__codelineno-7-12" > < / a > < span class = "w" > < / span > < span class = "c1" > // 添加顶点< / span >
< a id = "__codelineno-7-13" name = "__codelineno-7-13" href = "#__codelineno-7-13" > < / a > < span class = "w" > < / span > < span class = "k" > foreach< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > val< / span > < span class = "w" > < / span > < span class = "k" > in< / span > < span class = "w" > < / span > < span class = "n" > vertices< / span > < span class = "p" > )< / span >
< a id = "__codelineno-7-14" name = "__codelineno-7-14" href = "#__codelineno-7-14" > < / a > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-7-15" name = "__codelineno-7-15" href = "#__codelineno-7-15" > < / a > < span class = "w" > < / span > < span class = "n" > addVertex< / span > < span class = "p" > (< / span > < span class = "n" > val< / span > < span class = "p" > );< / span >
< a id = "__codelineno-7-16" name = "__codelineno-7-16" href = "#__codelineno-7-16" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-7-17" name = "__codelineno-7-17" href = "#__codelineno-7-17" > < / a > < span class = "w" > < / span > < span class = "c1" > // 添加边< / span >
< a id = "__codelineno-7-18" name = "__codelineno-7-18" href = "#__codelineno-7-18" > < / a > < span class = "w" > < / span > < span class = "c1" > // 请注意, edges 元素代表顶点索引,即对应 vertices 元素索引< / span >
< a id = "__codelineno-7-19" name = "__codelineno-7-19" href = "#__codelineno-7-19" > < / a > < span class = "w" > < / span > < span class = "k" > foreach< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "kt" > int< / span > < span class = "p" > []< / span > < span class = "w" > < / span > < span class = "n" > e< / span > < span class = "w" > < / span > < span class = "k" > in< / span > < span class = "w" > < / span > < span class = "n" > edges< / span > < span class = "p" > )< / span >
< a id = "__codelineno-7-20" name = "__codelineno-7-20" href = "#__codelineno-7-20" > < / a > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-7-21" name = "__codelineno-7-21" href = "#__codelineno-7-21" > < / a > < span class = "w" > < / span > < span class = "n" > addEdge< / span > < span class = "p" > (< / span > < span class = "n" > e< / span > < span class = "p" > [< / span > < span class = "m" > 0< / span > < span class = "p" > ],< / span > < span class = "w" > < / span > < span class = "n" > e< / span > < span class = "p" > [< / span > < span class = "m" > 1< / span > < span class = "p" > ]);< / span >
< a id = "__codelineno-7-22" name = "__codelineno-7-22" href = "#__codelineno-7-22" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-7-23" name = "__codelineno-7-23" href = "#__codelineno-7-23" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-7-24" name = "__codelineno-7-24" href = "#__codelineno-7-24" > < / a >
< a id = "__codelineno-7-25" name = "__codelineno-7-25" href = "#__codelineno-7-25" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 获取顶点数量 */< / span >
< a id = "__codelineno-7-26" name = "__codelineno-7-26" href = "#__codelineno-7-26" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "nf" > size< / span > < span class = "p" > ()< / span >
< a id = "__codelineno-7-27" name = "__codelineno-7-27" href = "#__codelineno-7-27" > < / a > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-7-28" name = "__codelineno-7-28" href = "#__codelineno-7-28" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "w" > < / span > < span class = "n" > vertices< / span > < span class = "p" > .< / span > < span class = "n" > Count< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-7-29" name = "__codelineno-7-29" href = "#__codelineno-7-29" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-7-30" name = "__codelineno-7-30" href = "#__codelineno-7-30" > < / a >
< a id = "__codelineno-7-31" name = "__codelineno-7-31" href = "#__codelineno-7-31" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 添加顶点 */< / span >
< a id = "__codelineno-7-32" name = "__codelineno-7-32" href = "#__codelineno-7-32" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "k" > void< / span > < span class = "w" > < / span > < span class = "nf" > addVertex< / span > < span class = "p" > (< / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > val< / span > < span class = "p" > )< / span >
< a id = "__codelineno-7-33" name = "__codelineno-7-33" href = "#__codelineno-7-33" > < / a > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-7-34" name = "__codelineno-7-34" href = "#__codelineno-7-34" > < / a > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > n< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > size< / span > < span class = "p" > ();< / span >
< a id = "__codelineno-7-35" name = "__codelineno-7-35" href = "#__codelineno-7-35" > < / a > < span class = "w" > < / span > < span class = "c1" > // 向顶点列表中添加新顶点的值< / span >
< a id = "__codelineno-7-36" name = "__codelineno-7-36" href = "#__codelineno-7-36" > < / a > < span class = "w" > < / span > < span class = "n" > vertices< / span > < span class = "p" > .< / span > < span class = "n" > Add< / span > < span class = "p" > (< / span > < span class = "n" > val< / span > < span class = "p" > );< / span >
< a id = "__codelineno-7-37" name = "__codelineno-7-37" href = "#__codelineno-7-37" > < / a > < span class = "w" > < / span > < span class = "c1" > // 在邻接矩阵中添加一行< / span >
< a id = "__codelineno-7-38" name = "__codelineno-7-38" href = "#__codelineno-7-38" > < / a > < span class = "w" > < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "kt" > int< / span > < span class = "o" > > < / span > < span class = "w" > < / span > < span class = "n" > newRow< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "k" > new< / span > < span class = "w" > < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "kt" > int< / span > < span class = "o" > > < / span > < span class = "p" > (< / span > < span class = "n" > n< / span > < span class = "p" > );< / span >
< a id = "__codelineno-7-39" name = "__codelineno-7-39" href = "#__codelineno-7-39" > < / a > < span class = "w" > < / span > < span class = "k" > for< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > j< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "m" > 0< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "n" > j< / span > < span class = "w" > < / span > < span class = "o" > < < / span > < span class = "w" > < / span > < span class = "n" > n< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "n" > j< / span > < span class = "o" > ++< / span > < span class = "p" > )< / span >
< a id = "__codelineno-7-40" name = "__codelineno-7-40" href = "#__codelineno-7-40" > < / a > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-7-41" name = "__codelineno-7-41" href = "#__codelineno-7-41" > < / a > < span class = "w" > < / span > < span class = "n" > newRow< / span > < span class = "p" > .< / span > < span class = "n" > Add< / span > < span class = "p" > (< / span > < span class = "m" > 0< / span > < span class = "p" > );< / span >
< a id = "__codelineno-7-42" name = "__codelineno-7-42" href = "#__codelineno-7-42" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-7-43" name = "__codelineno-7-43" href = "#__codelineno-7-43" > < / a > < span class = "w" > < / span > < span class = "n" > adjMat< / span > < span class = "p" > .< / span > < span class = "n" > Add< / span > < span class = "p" > (< / span > < span class = "n" > newRow< / span > < span class = "p" > );< / span >
< a id = "__codelineno-7-44" name = "__codelineno-7-44" href = "#__codelineno-7-44" > < / a > < span class = "w" > < / span > < span class = "c1" > // 在邻接矩阵中添加一列< / span >
< a id = "__codelineno-7-45" name = "__codelineno-7-45" href = "#__codelineno-7-45" > < / a > < span class = "w" > < / span > < span class = "k" > foreach< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "kt" > int< / span > < span class = "o" > > < / span > < span class = "w" > < / span > < span class = "n" > row< / span > < span class = "w" > < / span > < span class = "k" > in< / span > < span class = "w" > < / span > < span class = "n" > adjMat< / span > < span class = "p" > )< / span >
< a id = "__codelineno-7-46" name = "__codelineno-7-46" href = "#__codelineno-7-46" > < / a > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-7-47" name = "__codelineno-7-47" href = "#__codelineno-7-47" > < / a > < span class = "w" > < / span > < span class = "n" > row< / span > < span class = "p" > .< / span > < span class = "n" > Add< / span > < span class = "p" > (< / span > < span class = "m" > 0< / span > < span class = "p" > );< / span >
< a id = "__codelineno-7-48" name = "__codelineno-7-48" href = "#__codelineno-7-48" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-7-49" name = "__codelineno-7-49" href = "#__codelineno-7-49" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-7-50" name = "__codelineno-7-50" href = "#__codelineno-7-50" > < / a >
< a id = "__codelineno-7-51" name = "__codelineno-7-51" href = "#__codelineno-7-51" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 删除顶点 */< / span >
< a id = "__codelineno-7-52" name = "__codelineno-7-52" href = "#__codelineno-7-52" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "k" > void< / span > < span class = "w" > < / span > < span class = "nf" > removeVertex< / span > < span class = "p" > (< / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > index< / span > < span class = "p" > )< / span >
< a id = "__codelineno-7-53" name = "__codelineno-7-53" href = "#__codelineno-7-53" > < / a > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-7-54" name = "__codelineno-7-54" href = "#__codelineno-7-54" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > index< / span > < span class = "w" > < / span > < span class = "o" > > =< / span > < span class = "w" > < / span > < span class = "n" > size< / span > < span class = "p" > ())< / span >
< a id = "__codelineno-7-55" name = "__codelineno-7-55" href = "#__codelineno-7-55" > < / a > < span class = "w" > < / span > < span class = "k" > throw< / span > < span class = "w" > < / span > < span class = "k" > new< / span > < span class = "w" > < / span > < span class = "nf" > IndexOutOfRangeException< / span > < span class = "p" > ();< / span >
< a id = "__codelineno-7-56" name = "__codelineno-7-56" href = "#__codelineno-7-56" > < / a > < span class = "w" > < / span > < span class = "c1" > // 在顶点列表中移除索引 index 的顶点< / span >
< a id = "__codelineno-7-57" name = "__codelineno-7-57" href = "#__codelineno-7-57" > < / a > < span class = "w" > < / span > < span class = "n" > vertices< / span > < span class = "p" > .< / span > < span class = "n" > RemoveAt< / span > < span class = "p" > (< / span > < span class = "n" > index< / span > < span class = "p" > );< / span >
< a id = "__codelineno-7-58" name = "__codelineno-7-58" href = "#__codelineno-7-58" > < / a > < span class = "w" > < / span > < span class = "c1" > // 在邻接矩阵中删除索引 index 的行< / span >
< a id = "__codelineno-7-59" name = "__codelineno-7-59" href = "#__codelineno-7-59" > < / a > < span class = "w" > < / span > < span class = "n" > adjMat< / span > < span class = "p" > .< / span > < span class = "n" > RemoveAt< / span > < span class = "p" > (< / span > < span class = "n" > index< / span > < span class = "p" > );< / span >
< a id = "__codelineno-7-60" name = "__codelineno-7-60" href = "#__codelineno-7-60" > < / a > < span class = "w" > < / span > < span class = "c1" > // 在邻接矩阵中删除索引 index 的列< / span >
< a id = "__codelineno-7-61" name = "__codelineno-7-61" href = "#__codelineno-7-61" > < / a > < span class = "w" > < / span > < span class = "k" > foreach< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "kt" > int< / span > < span class = "o" > > < / span > < span class = "w" > < / span > < span class = "n" > row< / span > < span class = "w" > < / span > < span class = "k" > in< / span > < span class = "w" > < / span > < span class = "n" > adjMat< / span > < span class = "p" > )< / span >
< a id = "__codelineno-7-62" name = "__codelineno-7-62" href = "#__codelineno-7-62" > < / a > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-7-63" name = "__codelineno-7-63" href = "#__codelineno-7-63" > < / a > < span class = "w" > < / span > < span class = "n" > row< / span > < span class = "p" > .< / span > < span class = "n" > RemoveAt< / span > < span class = "p" > (< / span > < span class = "n" > index< / span > < span class = "p" > );< / span >
< a id = "__codelineno-7-64" name = "__codelineno-7-64" href = "#__codelineno-7-64" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-7-65" name = "__codelineno-7-65" href = "#__codelineno-7-65" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-7-66" name = "__codelineno-7-66" href = "#__codelineno-7-66" > < / a >
< a id = "__codelineno-7-67" name = "__codelineno-7-67" href = "#__codelineno-7-67" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 添加边 */< / span >
< a id = "__codelineno-7-68" name = "__codelineno-7-68" href = "#__codelineno-7-68" > < / a > < span class = "w" > < / span > < span class = "c1" > // 参数 i, j 对应 vertices 元素索引< / span >
< a id = "__codelineno-7-69" name = "__codelineno-7-69" href = "#__codelineno-7-69" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "k" > void< / span > < span class = "w" > < / span > < span class = "nf" > addEdge< / span > < span class = "p" > (< / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > i< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > j< / span > < span class = "p" > )< / span >
< a id = "__codelineno-7-70" name = "__codelineno-7-70" href = "#__codelineno-7-70" > < / a > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-7-71" name = "__codelineno-7-71" href = "#__codelineno-7-71" > < / a > < span class = "w" > < / span > < span class = "c1" > // 索引越界与相等处理< / span >
< a id = "__codelineno-7-72" name = "__codelineno-7-72" href = "#__codelineno-7-72" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > i< / span > < span class = "w" > < / span > < span class = "o" > < < / span > < span class = "w" > < / span > < span class = "m" > 0< / span > < span class = "w" > < / span > < span class = "o" > ||< / span > < span class = "w" > < / span > < span class = "n" > j< / span > < span class = "w" > < / span > < span class = "o" > < < / span > < span class = "w" > < / span > < span class = "m" > 0< / span > < span class = "w" > < / span > < span class = "o" > ||< / span > < span class = "w" > < / span > < span class = "n" > i< / span > < span class = "w" > < / span > < span class = "o" > > =< / span > < span class = "w" > < / span > < span class = "n" > size< / span > < span class = "p" > ()< / span > < span class = "w" > < / span > < span class = "o" > ||< / span > < span class = "w" > < / span > < span class = "n" > j< / span > < span class = "w" > < / span > < span class = "o" > > =< / span > < span class = "w" > < / span > < span class = "n" > size< / span > < span class = "p" > ()< / span > < span class = "w" > < / span > < span class = "o" > ||< / span > < span class = "w" > < / span > < span class = "n" > i< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "n" > j< / span > < span class = "p" > )< / span >
< a id = "__codelineno-7-73" name = "__codelineno-7-73" href = "#__codelineno-7-73" > < / a > < span class = "w" > < / span > < span class = "k" > throw< / span > < span class = "w" > < / span > < span class = "k" > new< / span > < span class = "w" > < / span > < span class = "nf" > IndexOutOfRangeException< / span > < span class = "p" > ();< / span >
< a id = "__codelineno-7-74" name = "__codelineno-7-74" href = "#__codelineno-7-74" > < / a > < span class = "w" > < / span > < span class = "c1" > // 在无向图中,邻接矩阵沿主对角线对称,即满足 (i, j) == (j, i)< / span >
< a id = "__codelineno-7-75" name = "__codelineno-7-75" href = "#__codelineno-7-75" > < / a > < span class = "w" > < / span > < span class = "n" > adjMat< / span > < span class = "p" > [< / span > < span class = "n" > i< / span > < span class = "p" > ][< / span > < span class = "n" > j< / span > < span class = "p" > ]< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "m" > 1< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-7-76" name = "__codelineno-7-76" href = "#__codelineno-7-76" > < / a > < span class = "w" > < / span > < span class = "n" > adjMat< / span > < span class = "p" > [< / span > < span class = "n" > j< / span > < span class = "p" > ][< / span > < span class = "n" > i< / span > < span class = "p" > ]< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "m" > 1< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-7-77" name = "__codelineno-7-77" href = "#__codelineno-7-77" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-7-78" name = "__codelineno-7-78" href = "#__codelineno-7-78" > < / a >
< a id = "__codelineno-7-79" name = "__codelineno-7-79" href = "#__codelineno-7-79" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 删除边 */< / span >
< a id = "__codelineno-7-80" name = "__codelineno-7-80" href = "#__codelineno-7-80" > < / a > < span class = "w" > < / span > < span class = "c1" > // 参数 i, j 对应 vertices 元素索引< / span >
< a id = "__codelineno-7-81" name = "__codelineno-7-81" href = "#__codelineno-7-81" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "k" > void< / span > < span class = "w" > < / span > < span class = "nf" > removeEdge< / span > < span class = "p" > (< / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > i< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > j< / span > < span class = "p" > )< / span >
< a id = "__codelineno-7-82" name = "__codelineno-7-82" href = "#__codelineno-7-82" > < / a > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-7-83" name = "__codelineno-7-83" href = "#__codelineno-7-83" > < / a > < span class = "w" > < / span > < span class = "c1" > // 索引越界与相等处理< / span >
< a id = "__codelineno-7-84" name = "__codelineno-7-84" href = "#__codelineno-7-84" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > i< / span > < span class = "w" > < / span > < span class = "o" > < < / span > < span class = "w" > < / span > < span class = "m" > 0< / span > < span class = "w" > < / span > < span class = "o" > ||< / span > < span class = "w" > < / span > < span class = "n" > j< / span > < span class = "w" > < / span > < span class = "o" > < < / span > < span class = "w" > < / span > < span class = "m" > 0< / span > < span class = "w" > < / span > < span class = "o" > ||< / span > < span class = "w" > < / span > < span class = "n" > i< / span > < span class = "w" > < / span > < span class = "o" > > =< / span > < span class = "w" > < / span > < span class = "n" > size< / span > < span class = "p" > ()< / span > < span class = "w" > < / span > < span class = "o" > ||< / span > < span class = "w" > < / span > < span class = "n" > j< / span > < span class = "w" > < / span > < span class = "o" > > =< / span > < span class = "w" > < / span > < span class = "n" > size< / span > < span class = "p" > ()< / span > < span class = "w" > < / span > < span class = "o" > ||< / span > < span class = "w" > < / span > < span class = "n" > i< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "n" > j< / span > < span class = "p" > )< / span >
< a id = "__codelineno-7-85" name = "__codelineno-7-85" href = "#__codelineno-7-85" > < / a > < span class = "w" > < / span > < span class = "k" > throw< / span > < span class = "w" > < / span > < span class = "k" > new< / span > < span class = "w" > < / span > < span class = "nf" > IndexOutOfRangeException< / span > < span class = "p" > ();< / span >
< a id = "__codelineno-7-86" name = "__codelineno-7-86" href = "#__codelineno-7-86" > < / a > < span class = "w" > < / span > < span class = "n" > adjMat< / span > < span class = "p" > [< / span > < span class = "n" > i< / span > < span class = "p" > ][< / span > < span class = "n" > j< / span > < span class = "p" > ]< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "m" > 0< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-7-87" name = "__codelineno-7-87" href = "#__codelineno-7-87" > < / a > < span class = "w" > < / span > < span class = "n" > adjMat< / span > < span class = "p" > [< / span > < span class = "n" > j< / span > < span class = "p" > ][< / span > < span class = "n" > i< / span > < span class = "p" > ]< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "m" > 0< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-7-88" name = "__codelineno-7-88" href = "#__codelineno-7-88" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-7-89" name = "__codelineno-7-89" href = "#__codelineno-7-89" > < / a >
< a id = "__codelineno-7-90" name = "__codelineno-7-90" href = "#__codelineno-7-90" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 打印邻接矩阵 */< / span >
< a id = "__codelineno-7-91" name = "__codelineno-7-91" href = "#__codelineno-7-91" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "k" > void< / span > < span class = "w" > < / span > < span class = "nf" > print< / span > < span class = "p" > ()< / span >
< a id = "__codelineno-7-92" name = "__codelineno-7-92" href = "#__codelineno-7-92" > < / a > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-7-93" name = "__codelineno-7-93" href = "#__codelineno-7-93" > < / a > < span class = "w" > < / span > < span class = "n" > Console< / span > < span class = "p" > .< / span > < span class = "n" > Write< / span > < span class = "p" > (< / span > < span class = "s" > " 顶点列表 = " < / span > < span class = "p" > );< / span >
< a id = "__codelineno-7-94" name = "__codelineno-7-94" href = "#__codelineno-7-94" > < / a > < span class = "w" > < / span > < span class = "n" > PrintUtil< / span > < span class = "p" > .< / span > < span class = "n" > PrintList< / span > < span class = "p" > (< / span > < span class = "n" > vertices< / span > < span class = "p" > );< / span >
< a id = "__codelineno-7-95" name = "__codelineno-7-95" href = "#__codelineno-7-95" > < / a > < span class = "w" > < / span > < span class = "n" > Console< / span > < span class = "p" > .< / span > < span class = "n" > WriteLine< / span > < span class = "p" > (< / span > < span class = "s" > " 邻接矩阵 =" < / span > < span class = "p" > );< / span >
< a id = "__codelineno-7-96" name = "__codelineno-7-96" href = "#__codelineno-7-96" > < / a > < span class = "w" > < / span > < span class = "n" > PrintUtil< / span > < span class = "p" > .< / span > < span class = "n" > printMatrix< / span > < span class = "p" > (< / span > < span class = "n" > adjMat< / span > < span class = "p" > );< / span >
< a id = "__codelineno-7-97" name = "__codelineno-7-97" href = "#__codelineno-7-97" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-7-98" name = "__codelineno-7-98" href = "#__codelineno-7-98" > < / a > < span class = "p" > }< / span >
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
@ -2213,75 +2310,74 @@
< div class = "tabbed-block" >
< div class = "highlight" > < span class = "filename" > graph_adjacency_list.java< / span > < pre > < span > < / span > < code > < a id = "__codelineno-10-1" name = "__codelineno-10-1" href = "#__codelineno-10-1" > < / a > < span class = "cm" > /* 基于邻接表实现的无向图类 */< / span >
< a id = "__codelineno-10-2" name = "__codelineno-10-2" href = "#__codelineno-10-2" > < / a > < span class = "kd" > class< / span > < span class = "nc" > GraphAdjList< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-10-3" name = "__codelineno-10-3" href = "#__codelineno-10-3" > < / a > < span class = "w" > < / span > < span class = "c1" > // 邻接表,使用哈希表来代替链表,以提升删除边、删除顶点的效率< / span >
< a id = "__codelineno-10-4" name = "__codelineno-10-4" href = "#__codelineno-10-4" > < / a > < span class = "w" > < / span > < span class = "c1" > // 请注意, adjList 中的元素是 Vertex 对象< / span >
< a id = "__codelineno-10-5" name = "__codelineno-10-5" href = "#__codelineno-10-5" > < / a > < span class = "w" > < / span > < span class = "n" > Map< / span > < span class = "o" > < < / span > < span class = "n" > Vertex< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > Vertex< / span > < span class = "o" > > > < / span > < span class = "w" > < / span > < span class = "n" > adjList< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-10-6" name = "__codelineno-10-6" href = "#__codelineno-10-6" > < / a >
< a id = "__codelineno-10-7" name = "__codelineno-10-7" href = "#__codelineno-10-7" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 构造方法 */< / span >
< a id = "__codelineno-10-8" name = "__codelineno-10-8" href = "#__codelineno-10-8" > < / a > < span class = "w" > < / span > < span class = "kd" > public< / span > < span class = "w" > < / span > < span class = "nf" > GraphAdjList< / span > < span class = "p" > (< / span > < span class = "n" > Vertex< / span > < span class = "o" > [][]< / span > < span class = "w" > < / span > < span class = "n" > edges< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-10-9" name = "__codelineno-10-9" href = "#__codelineno-10-9" > < / a > < span class = "w" > < / span > < span class = "k" > this< / span > < span class = "p" > .< / span > < span class = "na" > adjList< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "k" > new< / span > < span class = "w" > < / span > < span class = "n" > HashMap< / span > < span class = "o" > < > < / span > < span class = "p" > ();< / span >
< a id = "__codelineno-10-10" name = "__codelineno-10-10" href = "#__codelineno-10-10" > < / a > < span class = "w" > < / span > < span class = "c1" > // 添加所有顶点和边< / span >
< a id = "__codelineno-10-11" name = "__codelineno-10-11" href = "#__codelineno-10-11" > < / a > < span class = "w" > < / span > < span class = "k" > for< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > Vertex< / span > < span class = "o" > []< / span > < span class = "w" > < / span > < span class = "n" > edge< / span > < span class = "w" > < / span > < span class = "p" > :< / span > < span class = "w" > < / span > < span class = "n" > edges< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-10-12" name = "__codelineno-10-12" href = "#__codelineno-10-12" > < / a > < span class = "w" > < / span > < span class = "n" > addVertex< / span > < span class = "p" > (< / span > < span class = "n" > edge< / span > < span class = "o" > [< / span > < span class = "mi" > 0< / span > < span class = "o" > ]< / span > < span class = "p" > );< / span >
< a id = "__codelineno-10-13" name = "__codelineno-10-13" href = "#__codelineno-10-13" > < / a > < span class = "w" > < / span > < span class = "n" > addVertex< / span > < span class = "p" > (< / span > < span class = "n" > edge< / span > < span class = "o" > [< / span > < span class = "mi" > 1< / span > < span class = "o" > ]< / span > < span class = "p" > );< / span >
< a id = "__codelineno-10-14" name = "__codelineno-10-14" href = "#__codelineno-10-14" > < / a > < span class = "w" > < / span > < span class = "n" > addEdge< / span > < span class = "p" > (< / span > < span class = "n" > edge< / span > < span class = "o" > [< / span > < span class = "mi" > 0< / span > < span class = "o" > ]< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > edge< / span > < span class = "o" > [< / span > < span class = "mi" > 1< / span > < span class = "o" > ]< / span > < span class = "p" > );< / span >
< a id = "__codelineno-10-15" name = "__codelineno-10-15" href = "#__codelineno-10-15" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-10-16" name = "__codelineno-10-16" href = "#__codelineno-10-16" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-10-17" name = "__codelineno-10-17" href = "#__codelineno-10-17" > < / a >
< a id = "__codelineno-10-18" name = "__codelineno-10-18" href = "#__codelineno-10-18" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 获取顶点数量 */< / span >
< a id = "__codelineno-10-19" name = "__codelineno-10-19" href = "#__codelineno-10-19" > < / a > < span class = "w" > < / span > < span class = "kd" > public< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "nf" > size< / span > < span class = "p" > ()< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-10-20" name = "__codelineno-10-20" href = "#__codelineno-10-20" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "w" > < / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "na" > size< / span > < span class = "p" > ();< / span >
< a id = "__codelineno-10-21" name = "__codelineno-10-21" href = "#__codelineno-10-21" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-10-22" name = "__codelineno-10-22" href = "#__codelineno-10-22" > < / a >
< a id = "__codelineno-10-23" name = "__codelineno-10-23" href = "#__codelineno-10-23" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 添加边 */< / span >
< a id = "__codelineno-10-24" name = "__codelineno-10-24" href = "#__codelineno-10-24" > < / a > < span class = "w" > < / span > < span class = "kd" > public< / span > < span class = "w" > < / span > < span class = "kt" > void< / span > < span class = "w" > < / span > < span class = "nf" > addEdge< / span > < span class = "p" > (< / span > < span class = "n" > Vertex< / span > < span class = "w" > < / span > < span class = "n" > vet1< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > Vertex< / span > < span class = "w" > < / span > < span class = "n" > vet2< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-10-25" name = "__codelineno-10-25" href = "#__codelineno-10-25" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "o" > !< / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "na" > containsKey< / span > < span class = "p" > (< / span > < span class = "n" > vet1< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "o" > ||< / span > < span class = "w" > < / span > < span class = "o" > !< / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "na" > containsKey< / span > < span class = "p" > (< / span > < span class = "n" > vet2< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "o" > ||< / span > < span class = "w" > < / span > < span class = "n" > vet1< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "n" > vet2< / span > < span class = "p" > )< / span >
< a id = "__codelineno-10-26" name = "__codelineno-10-26" href = "#__codelineno-10-26" > < / a > < span class = "w" > < / span > < span class = "k" > throw< / span > < span class = "w" > < / span > < span class = "k" > new< / span > < span class = "w" > < / span > < span class = "n" > IllegalArgumentException< / span > < span class = "p" > ();< / span >
< a id = "__codelineno-10-27" name = "__codelineno-10-27" href = "#__codelineno-10-27" > < / a > < span class = "w" > < / span > < span class = "c1" > // 添加边 vet1 - vet2< / span >
< a id = "__codelineno-10-28" name = "__codelineno-10-28" href = "#__codelineno-10-28" > < / a > < span class = "w" > < / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "na" > get< / span > < span class = "p" > (< / span > < span class = "n" > vet1< / span > < span class = "p" > ).< / span > < span class = "na" > add< / span > < span class = "p" > (< / span > < span class = "n" > vet2< / span > < span class = "p" > );< / span >
< a id = "__codelineno-10-29" name = "__codelineno-10-29" href = "#__codelineno-10-29" > < / a > < span class = "w" > < / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "na" > get< / span > < span class = "p" > (< / span > < span class = "n" > vet2< / span > < span class = "p" > ).< / span > < span class = "na" > add< / span > < span class = "p" > (< / span > < span class = "n" > vet1< / span > < span class = "p" > );< / span >
< a id = "__codelineno-10-30" name = "__codelineno-10-30" href = "#__codelineno-10-30" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-10-31" name = "__codelineno-10-31" href = "#__codelineno-10-31" > < / a >
< a id = "__codelineno-10-32" name = "__codelineno-10-32" href = "#__codelineno-10-32" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 删除边 */< / span >
< a id = "__codelineno-10-33" name = "__codelineno-10-33" href = "#__codelineno-10-33" > < / a > < span class = "w" > < / span > < span class = "kd" > public< / span > < span class = "w" > < / span > < span class = "kt" > void< / span > < span class = "w" > < / span > < span class = "nf" > removeEdge< / span > < span class = "p" > (< / span > < span class = "n" > Vertex< / span > < span class = "w" > < / span > < span class = "n" > vet1< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > Vertex< / span > < span class = "w" > < / span > < span class = "n" > vet2< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-10-34" name = "__codelineno-10-34" href = "#__codelineno-10-34" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "o" > !< / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "na" > containsKey< / span > < span class = "p" > (< / span > < span class = "n" > vet1< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "o" > ||< / span > < span class = "w" > < / span > < span class = "o" > !< / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "na" > containsKey< / span > < span class = "p" > (< / span > < span class = "n" > vet2< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "o" > ||< / span > < span class = "w" > < / span > < span class = "n" > vet1< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "n" > vet2< / span > < span class = "p" > )< / span >
< a id = "__codelineno-10-35" name = "__codelineno-10-35" href = "#__codelineno-10-35" > < / a > < span class = "w" > < / span > < span class = "k" > throw< / span > < span class = "w" > < / span > < span class = "k" > new< / span > < span class = "w" > < / span > < span class = "n" > IllegalArgumentException< / span > < span class = "p" > ();< / span >
< a id = "__codelineno-10-36" name = "__codelineno-10-36" href = "#__codelineno-10-36" > < / a > < span class = "w" > < / span > < span class = "c1" > // 删除边 vet1 - vet2< / span >
< a id = "__codelineno-10-37" name = "__codelineno-10-37" href = "#__codelineno-10-37" > < / a > < span class = "w" > < / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "na" > get< / span > < span class = "p" > (< / span > < span class = "n" > vet1< / span > < span class = "p" > ).< / span > < span class = "na" > remove< / span > < span class = "p" > (< / span > < span class = "n" > vet2< / span > < span class = "p" > );< / span >
< a id = "__codelineno-10-38" name = "__codelineno-10-38" href = "#__codelineno-10-38" > < / a > < span class = "w" > < / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "na" > get< / span > < span class = "p" > (< / span > < span class = "n" > vet2< / span > < span class = "p" > ).< / span > < span class = "na" > remove< / span > < span class = "p" > (< / span > < span class = "n" > vet1< / span > < span class = "p" > );< / span >
< a id = "__codelineno-10-39" name = "__codelineno-10-39" href = "#__codelineno-10-39" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-10-40" name = "__codelineno-10-40" href = "#__codelineno-10-40" > < / a >
< a id = "__codelineno-10-41" name = "__codelineno-10-41" href = "#__codelineno-10-41" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 添加顶点 */< / span >
< a id = "__codelineno-10-42" name = "__codelineno-10-42" href = "#__codelineno-10-42" > < / a > < span class = "w" > < / span > < span class = "kd" > public< / span > < span class = "w" > < / span > < span class = "kt" > void< / span > < span class = "w" > < / span > < span class = "nf" > addVertex< / span > < span class = "p" > (< / span > < span class = "n" > Vertex< / span > < span class = "w" > < / span > < span class = "n" > vet< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-10-43" name = "__codelineno-10-43" href = "#__codelineno-10-43" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "na" > containsKey< / span > < span class = "p" > (< / span > < span class = "n" > vet< / span > < span class = "p" > ))< / span >
< a id = "__codelineno-10-44" name = "__codelineno-10-44" href = "#__codelineno-10-44" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-10-45" name = "__codelineno-10-45" href = "#__codelineno-10-45" > < / a > < span class = "w" > < / span > < span class = "c1" > // 在邻接表中添加一个新链表< / span >
< a id = "__codelineno-10-46" name = "__codelineno-10-46" href = "#__codelineno-10-46" > < / a > < span class = "w" > < / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "na" > put< / span > < span class = "p" > (< / span > < span class = "n" > vet< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "k" > new< / span > < span class = "w" > < / span > < span class = "n" > ArrayList< / span > < span class = "o" > < > < / span > < span class = "p" > ());< / span >
< a id = "__codelineno-10-47" name = "__codelineno-10-47" href = "#__codelineno-10-47" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-10-48" name = "__codelineno-10-48" href = "#__codelineno-10-48" > < / a >
< a id = "__codelineno-10-49" name = "__codelineno-10-49" href = "#__codelineno-10-49" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 删除顶点 */< / span >
< a id = "__codelineno-10-50" name = "__codelineno-10-50" href = "#__codelineno-10-50" > < / a > < span class = "w" > < / span > < span class = "kd" > public< / span > < span class = "w" > < / span > < span class = "kt" > void< / span > < span class = "w" > < / span > < span class = "nf" > removeVertex< / span > < span class = "p" > (< / span > < span class = "n" > Vertex< / span > < span class = "w" > < / span > < span class = "n" > vet< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-10-51" name = "__codelineno-10-51" href = "#__codelineno-10-51" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "o" > !< / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "na" > containsKey< / span > < span class = "p" > (< / span > < span class = "n" > vet< / span > < span class = "p" > ))< / span >
< a id = "__codelineno-10-52" name = "__codelineno-10-52" href = "#__codelineno-10-52" > < / a > < span class = "w" > < / span > < span class = "k" > throw< / span > < span class = "w" > < / span > < span class = "k" > new< / span > < span class = "w" > < / span > < span class = "n" > IllegalArgumentException< / span > < span class = "p" > ();< / span >
< a id = "__codelineno-10-53" name = "__codelineno-10-53" href = "#__codelineno-10-53" > < / a > < span class = "w" > < / span > < span class = "c1" > // 在邻接表中删除顶点 vet 对应的链表< / span >
< a id = "__codelineno-10-54" name = "__codelineno-10-54" href = "#__codelineno-10-54" > < / a > < span class = "w" > < / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "na" > remove< / span > < span class = "p" > (< / span > < span class = "n" > vet< / span > < span class = "p" > );< / span >
< a id = "__codelineno-10-55" name = "__codelineno-10-55" href = "#__codelineno-10-55" > < / a > < span class = "w" > < / span > < span class = "c1" > // 遍历其它顶点的链表,删除所有包含 vet 的边< / span >
< a id = "__codelineno-10-56" name = "__codelineno-10-56" href = "#__codelineno-10-56" > < / a > < span class = "w" > < / span > < span class = "k" > for< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > Vertex< / span > < span class = "o" > > < / span > < span class = "w" > < / span > < span class = "n" > list< / span > < span class = "w" > < / span > < span class = "p" > :< / span > < span class = "w" > < / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "na" > values< / span > < span class = "p" > ())< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-10-57" name = "__codelineno-10-57" href = "#__codelineno-10-57" > < / a > < span class = "w" > < / span > < span class = "n" > list< / span > < span class = "p" > .< / span > < span class = "na" > remove< / span > < span class = "p" > (< / span > < span class = "n" > vet< / span > < span class = "p" > );< / span >
< a id = "__codelineno-10-58" name = "__codelineno-10-58" href = "#__codelineno-10-58" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-10-59" name = "__codelineno-10-59" href = "#__codelineno-10-59" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-10-60" name = "__codelineno-10-60" href = "#__codelineno-10-60" > < / a >
< a id = "__codelineno-10-61" name = "__codelineno-10-61" href = "#__codelineno-10-61" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 打印邻接表 */< / span >
< a id = "__codelineno-10-62" name = "__codelineno-10-62" href = "#__codelineno-10-62" > < / a > < span class = "w" > < / span > < span class = "kd" > public< / span > < span class = "w" > < / span > < span class = "kt" > void< / span > < span class = "w" > < / span > < span class = "nf" > print< / span > < span class = "p" > ()< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-10-63" name = "__codelineno-10-63" href = "#__codelineno-10-63" > < / a > < span class = "w" > < / span > < span class = "n" > System< / span > < span class = "p" > .< / span > < span class = "na" > out< / span > < span class = "p" > .< / span > < span class = "na" > println< / span > < span class = "p" > (< / span > < span class = "s" > " 邻接表 =" < / span > < span class = "p" > );< / span >
< a id = "__codelineno-10-64" name = "__codelineno-10-64" href = "#__codelineno-10-64" > < / a > < span class = "w" > < / span > < span class = "k" > for< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > Map< / span > < span class = "p" > .< / span > < span class = "na" > Entry< / span > < span class = "o" > < < / span > < span class = "n" > Vertex< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > Vertex< / span > < span class = "o" > > > < / span > < span class = "w" > < / span > < span class = "n" > entry< / span > < span class = "w" > < / span > < span class = "p" > :< / span > < span class = "w" > < / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "na" > entrySet< / span > < span class = "p" > ())< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-10-65" name = "__codelineno-10-65" href = "#__codelineno-10-65" > < / a > < span class = "w" > < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > Integer< / span > < span class = "o" > > < / span > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "k" > new< / span > < span class = "w" > < / span > < span class = "n" > ArrayList< / span > < span class = "o" > < > < / span > < span class = "p" > ();< / span >
< a id = "__codelineno-10-66" name = "__codelineno-10-66" href = "#__codelineno-10-66" > < / a > < span class = "w" > < / span > < span class = "k" > for< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > Vertex< / span > < span class = "w" > < / span > < span class = "n" > vertex< / span > < span class = "w" > < / span > < span class = "p" > :< / span > < span class = "w" > < / span > < span class = "n" > entry< / span > < span class = "p" > .< / span > < span class = "na" > getValue< / span > < span class = "p" > ())< / span >
< a id = "__codelineno-10-67" name = "__codelineno-10-67" href = "#__codelineno-10-67" > < / a > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "p" > .< / span > < span class = "na" > add< / span > < span class = "p" > (< / span > < span class = "n" > vertex< / span > < span class = "p" > .< / span > < span class = "na" > val< / span > < span class = "p" > );< / span >
< a id = "__codelineno-10-68" name = "__codelineno-10-68" href = "#__codelineno-10-68" > < / a > < span class = "w" > < / span > < span class = "n" > System< / span > < span class = "p" > .< / span > < span class = "na" > out< / span > < span class = "p" > .< / span > < span class = "na" > println< / span > < span class = "p" > (< / span > < span class = "n" > entry< / span > < span class = "p" > .< / span > < span class = "na" > getKey< / span > < span class = "p" > ().< / span > < span class = "na" > val< / span > < span class = "w" > < / span > < span class = "o" > +< / span > < span class = "w" > < / span > < span class = "s" > " : " < / span > < span class = "w" > < / span > < span class = "o" > +< / span > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "w" > < / span > < span class = "o" > +< / span > < span class = "w" > < / span > < span class = "s" > " ," < / span > < span class = "p" > );< / span >
< a id = "__codelineno-10-69" name = "__codelineno-10-69" href = "#__codelineno-10-69" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-10-70" name = "__codelineno-10-70" href = "#__codelineno-10-70" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-10-71" name = "__codelineno-10-71" href = "#__codelineno-10-71" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-10-3" name = "__codelineno-10-3" href = "#__codelineno-10-3" > < / a > < span class = "w" > < / span > < span class = "c1" > // 邻接表, key: 顶点, value: 该顶点的所有邻接结点< / span >
< a id = "__codelineno-10-4" name = "__codelineno-10-4" href = "#__codelineno-10-4" > < / a > < span class = "w" > < / span > < span class = "n" > Map< / span > < span class = "o" > < < / span > < span class = "n" > Vertex< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > Vertex< / span > < span class = "o" > > > < / span > < span class = "w" > < / span > < span class = "n" > adjList< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-10-5" name = "__codelineno-10-5" href = "#__codelineno-10-5" > < / a >
< a id = "__codelineno-10-6" name = "__codelineno-10-6" href = "#__codelineno-10-6" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 构造方法 */< / span >
< a id = "__codelineno-10-7" name = "__codelineno-10-7" href = "#__codelineno-10-7" > < / a > < span class = "w" > < / span > < span class = "kd" > public< / span > < span class = "w" > < / span > < span class = "nf" > GraphAdjList< / span > < span class = "p" > (< / span > < span class = "n" > Vertex< / span > < span class = "o" > [][]< / span > < span class = "w" > < / span > < span class = "n" > edges< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-10-8" name = "__codelineno-10-8" href = "#__codelineno-10-8" > < / a > < span class = "w" > < / span > < span class = "k" > this< / span > < span class = "p" > .< / span > < span class = "na" > adjList< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "k" > new< / span > < span class = "w" > < / span > < span class = "n" > HashMap< / span > < span class = "o" > < > < / span > < span class = "p" > ();< / span >
< a id = "__codelineno-10-9" name = "__codelineno-10-9" href = "#__codelineno-10-9" > < / a > < span class = "w" > < / span > < span class = "c1" > // 添加所有顶点和边< / span >
< a id = "__codelineno-10-10" name = "__codelineno-10-10" href = "#__codelineno-10-10" > < / a > < span class = "w" > < / span > < span class = "k" > for< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > Vertex< / span > < span class = "o" > []< / span > < span class = "w" > < / span > < span class = "n" > edge< / span > < span class = "w" > < / span > < span class = "p" > :< / span > < span class = "w" > < / span > < span class = "n" > edges< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-10-11" name = "__codelineno-10-11" href = "#__codelineno-10-11" > < / a > < span class = "w" > < / span > < span class = "n" > addVertex< / span > < span class = "p" > (< / span > < span class = "n" > edge< / span > < span class = "o" > [< / span > < span class = "mi" > 0< / span > < span class = "o" > ]< / span > < span class = "p" > );< / span >
< a id = "__codelineno-10-12" name = "__codelineno-10-12" href = "#__codelineno-10-12" > < / a > < span class = "w" > < / span > < span class = "n" > addVertex< / span > < span class = "p" > (< / span > < span class = "n" > edge< / span > < span class = "o" > [< / span > < span class = "mi" > 1< / span > < span class = "o" > ]< / span > < span class = "p" > );< / span >
< a id = "__codelineno-10-13" name = "__codelineno-10-13" href = "#__codelineno-10-13" > < / a > < span class = "w" > < / span > < span class = "n" > addEdge< / span > < span class = "p" > (< / span > < span class = "n" > edge< / span > < span class = "o" > [< / span > < span class = "mi" > 0< / span > < span class = "o" > ]< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > edge< / span > < span class = "o" > [< / span > < span class = "mi" > 1< / span > < span class = "o" > ]< / span > < span class = "p" > );< / span >
< a id = "__codelineno-10-14" name = "__codelineno-10-14" href = "#__codelineno-10-14" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-10-15" name = "__codelineno-10-15" href = "#__codelineno-10-15" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-10-16" name = "__codelineno-10-16" href = "#__codelineno-10-16" > < / a >
< a id = "__codelineno-10-17" name = "__codelineno-10-17" href = "#__codelineno-10-17" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 获取顶点数量 */< / span >
< a id = "__codelineno-10-18" name = "__codelineno-10-18" href = "#__codelineno-10-18" > < / a > < span class = "w" > < / span > < span class = "kd" > public< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "nf" > size< / span > < span class = "p" > ()< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-10-19" name = "__codelineno-10-19" href = "#__codelineno-10-19" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "w" > < / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "na" > size< / span > < span class = "p" > ();< / span >
< a id = "__codelineno-10-20" name = "__codelineno-10-20" href = "#__codelineno-10-20" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-10-21" name = "__codelineno-10-21" href = "#__codelineno-10-21" > < / a >
< a id = "__codelineno-10-22" name = "__codelineno-10-22" href = "#__codelineno-10-22" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 添加边 */< / span >
< a id = "__codelineno-10-23" name = "__codelineno-10-23" href = "#__codelineno-10-23" > < / a > < span class = "w" > < / span > < span class = "kd" > public< / span > < span class = "w" > < / span > < span class = "kt" > void< / span > < span class = "w" > < / span > < span class = "nf" > addEdge< / span > < span class = "p" > (< / span > < span class = "n" > Vertex< / span > < span class = "w" > < / span > < span class = "n" > vet1< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > Vertex< / span > < span class = "w" > < / span > < span class = "n" > vet2< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-10-24" name = "__codelineno-10-24" href = "#__codelineno-10-24" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "o" > !< / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "na" > containsKey< / span > < span class = "p" > (< / span > < span class = "n" > vet1< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "o" > ||< / span > < span class = "w" > < / span > < span class = "o" > !< / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "na" > containsKey< / span > < span class = "p" > (< / span > < span class = "n" > vet2< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "o" > ||< / span > < span class = "w" > < / span > < span class = "n" > vet1< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "n" > vet2< / span > < span class = "p" > )< / span >
< a id = "__codelineno-10-25" name = "__codelineno-10-25" href = "#__codelineno-10-25" > < / a > < span class = "w" > < / span > < span class = "k" > throw< / span > < span class = "w" > < / span > < span class = "k" > new< / span > < span class = "w" > < / span > < span class = "n" > IllegalArgumentException< / span > < span class = "p" > ();< / span >
< a id = "__codelineno-10-26" name = "__codelineno-10-26" href = "#__codelineno-10-26" > < / a > < span class = "w" > < / span > < span class = "c1" > // 添加边 vet1 - vet2< / span >
< a id = "__codelineno-10-27" name = "__codelineno-10-27" href = "#__codelineno-10-27" > < / a > < span class = "w" > < / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "na" > get< / span > < span class = "p" > (< / span > < span class = "n" > vet1< / span > < span class = "p" > ).< / span > < span class = "na" > add< / span > < span class = "p" > (< / span > < span class = "n" > vet2< / span > < span class = "p" > );< / span >
< a id = "__codelineno-10-28" name = "__codelineno-10-28" href = "#__codelineno-10-28" > < / a > < span class = "w" > < / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "na" > get< / span > < span class = "p" > (< / span > < span class = "n" > vet2< / span > < span class = "p" > ).< / span > < span class = "na" > add< / span > < span class = "p" > (< / span > < span class = "n" > vet1< / span > < span class = "p" > );< / span >
< a id = "__codelineno-10-29" name = "__codelineno-10-29" href = "#__codelineno-10-29" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-10-30" name = "__codelineno-10-30" href = "#__codelineno-10-30" > < / a >
< a id = "__codelineno-10-31" name = "__codelineno-10-31" href = "#__codelineno-10-31" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 删除边 */< / span >
< a id = "__codelineno-10-32" name = "__codelineno-10-32" href = "#__codelineno-10-32" > < / a > < span class = "w" > < / span > < span class = "kd" > public< / span > < span class = "w" > < / span > < span class = "kt" > void< / span > < span class = "w" > < / span > < span class = "nf" > removeEdge< / span > < span class = "p" > (< / span > < span class = "n" > Vertex< / span > < span class = "w" > < / span > < span class = "n" > vet1< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > Vertex< / span > < span class = "w" > < / span > < span class = "n" > vet2< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-10-33" name = "__codelineno-10-33" href = "#__codelineno-10-33" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "o" > !< / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "na" > containsKey< / span > < span class = "p" > (< / span > < span class = "n" > vet1< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "o" > ||< / span > < span class = "w" > < / span > < span class = "o" > !< / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "na" > containsKey< / span > < span class = "p" > (< / span > < span class = "n" > vet2< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "o" > ||< / span > < span class = "w" > < / span > < span class = "n" > vet1< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "n" > vet2< / span > < span class = "p" > )< / span >
< a id = "__codelineno-10-34" name = "__codelineno-10-34" href = "#__codelineno-10-34" > < / a > < span class = "w" > < / span > < span class = "k" > throw< / span > < span class = "w" > < / span > < span class = "k" > new< / span > < span class = "w" > < / span > < span class = "n" > IllegalArgumentException< / span > < span class = "p" > ();< / span >
< a id = "__codelineno-10-35" name = "__codelineno-10-35" href = "#__codelineno-10-35" > < / a > < span class = "w" > < / span > < span class = "c1" > // 删除边 vet1 - vet2< / span >
< a id = "__codelineno-10-36" name = "__codelineno-10-36" href = "#__codelineno-10-36" > < / a > < span class = "w" > < / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "na" > get< / span > < span class = "p" > (< / span > < span class = "n" > vet1< / span > < span class = "p" > ).< / span > < span class = "na" > remove< / span > < span class = "p" > (< / span > < span class = "n" > vet2< / span > < span class = "p" > );< / span >
< a id = "__codelineno-10-37" name = "__codelineno-10-37" href = "#__codelineno-10-37" > < / a > < span class = "w" > < / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "na" > get< / span > < span class = "p" > (< / span > < span class = "n" > vet2< / span > < span class = "p" > ).< / span > < span class = "na" > remove< / span > < span class = "p" > (< / span > < span class = "n" > vet1< / span > < span class = "p" > );< / span >
< a id = "__codelineno-10-38" name = "__codelineno-10-38" href = "#__codelineno-10-38" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-10-39" name = "__codelineno-10-39" href = "#__codelineno-10-39" > < / a >
< a id = "__codelineno-10-40" name = "__codelineno-10-40" href = "#__codelineno-10-40" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 添加顶点 */< / span >
< a id = "__codelineno-10-41" name = "__codelineno-10-41" href = "#__codelineno-10-41" > < / a > < span class = "w" > < / span > < span class = "kd" > public< / span > < span class = "w" > < / span > < span class = "kt" > void< / span > < span class = "w" > < / span > < span class = "nf" > addVertex< / span > < span class = "p" > (< / span > < span class = "n" > Vertex< / span > < span class = "w" > < / span > < span class = "n" > vet< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-10-42" name = "__codelineno-10-42" href = "#__codelineno-10-42" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "na" > containsKey< / span > < span class = "p" > (< / span > < span class = "n" > vet< / span > < span class = "p" > ))< / span >
< a id = "__codelineno-10-43" name = "__codelineno-10-43" href = "#__codelineno-10-43" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-10-44" name = "__codelineno-10-44" href = "#__codelineno-10-44" > < / a > < span class = "w" > < / span > < span class = "c1" > // 在邻接表中添加一个新链表< / span >
< a id = "__codelineno-10-45" name = "__codelineno-10-45" href = "#__codelineno-10-45" > < / a > < span class = "w" > < / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "na" > put< / span > < span class = "p" > (< / span > < span class = "n" > vet< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "k" > new< / span > < span class = "w" > < / span > < span class = "n" > ArrayList< / span > < span class = "o" > < > < / span > < span class = "p" > ());< / span >
< a id = "__codelineno-10-46" name = "__codelineno-10-46" href = "#__codelineno-10-46" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-10-47" name = "__codelineno-10-47" href = "#__codelineno-10-47" > < / a >
< a id = "__codelineno-10-48" name = "__codelineno-10-48" href = "#__codelineno-10-48" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 删除顶点 */< / span >
< a id = "__codelineno-10-49" name = "__codelineno-10-49" href = "#__codelineno-10-49" > < / a > < span class = "w" > < / span > < span class = "kd" > public< / span > < span class = "w" > < / span > < span class = "kt" > void< / span > < span class = "w" > < / span > < span class = "nf" > removeVertex< / span > < span class = "p" > (< / span > < span class = "n" > Vertex< / span > < span class = "w" > < / span > < span class = "n" > vet< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-10-50" name = "__codelineno-10-50" href = "#__codelineno-10-50" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "o" > !< / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "na" > containsKey< / span > < span class = "p" > (< / span > < span class = "n" > vet< / span > < span class = "p" > ))< / span >
< a id = "__codelineno-10-51" name = "__codelineno-10-51" href = "#__codelineno-10-51" > < / a > < span class = "w" > < / span > < span class = "k" > throw< / span > < span class = "w" > < / span > < span class = "k" > new< / span > < span class = "w" > < / span > < span class = "n" > IllegalArgumentException< / span > < span class = "p" > ();< / span >
< a id = "__codelineno-10-52" name = "__codelineno-10-52" href = "#__codelineno-10-52" > < / a > < span class = "w" > < / span > < span class = "c1" > // 在邻接表中删除顶点 vet 对应的链表< / span >
< a id = "__codelineno-10-53" name = "__codelineno-10-53" href = "#__codelineno-10-53" > < / a > < span class = "w" > < / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "na" > remove< / span > < span class = "p" > (< / span > < span class = "n" > vet< / span > < span class = "p" > );< / span >
< a id = "__codelineno-10-54" name = "__codelineno-10-54" href = "#__codelineno-10-54" > < / a > < span class = "w" > < / span > < span class = "c1" > // 遍历其它顶点的链表,删除所有包含 vet 的边< / span >
< a id = "__codelineno-10-55" name = "__codelineno-10-55" href = "#__codelineno-10-55" > < / a > < span class = "w" > < / span > < span class = "k" > for< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > Vertex< / span > < span class = "o" > > < / span > < span class = "w" > < / span > < span class = "n" > list< / span > < span class = "w" > < / span > < span class = "p" > :< / span > < span class = "w" > < / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "na" > values< / span > < span class = "p" > ())< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-10-56" name = "__codelineno-10-56" href = "#__codelineno-10-56" > < / a > < span class = "w" > < / span > < span class = "n" > list< / span > < span class = "p" > .< / span > < span class = "na" > remove< / span > < span class = "p" > (< / span > < span class = "n" > vet< / span > < span class = "p" > );< / span >
< a id = "__codelineno-10-57" name = "__codelineno-10-57" href = "#__codelineno-10-57" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-10-58" name = "__codelineno-10-58" href = "#__codelineno-10-58" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-10-59" name = "__codelineno-10-59" href = "#__codelineno-10-59" > < / a >
< a id = "__codelineno-10-60" name = "__codelineno-10-60" href = "#__codelineno-10-60" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 打印邻接表 */< / span >
< a id = "__codelineno-10-61" name = "__codelineno-10-61" href = "#__codelineno-10-61" > < / a > < span class = "w" > < / span > < span class = "kd" > public< / span > < span class = "w" > < / span > < span class = "kt" > void< / span > < span class = "w" > < / span > < span class = "nf" > print< / span > < span class = "p" > ()< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-10-62" name = "__codelineno-10-62" href = "#__codelineno-10-62" > < / a > < span class = "w" > < / span > < span class = "n" > System< / span > < span class = "p" > .< / span > < span class = "na" > out< / span > < span class = "p" > .< / span > < span class = "na" > println< / span > < span class = "p" > (< / span > < span class = "s" > " 邻接表 =" < / span > < span class = "p" > );< / span >
< a id = "__codelineno-10-63" name = "__codelineno-10-63" href = "#__codelineno-10-63" > < / a > < span class = "w" > < / span > < span class = "k" > for< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > Map< / span > < span class = "p" > .< / span > < span class = "na" > Entry< / span > < span class = "o" > < < / span > < span class = "n" > Vertex< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > Vertex< / span > < span class = "o" > > > < / span > < span class = "w" > < / span > < span class = "n" > entry< / span > < span class = "w" > < / span > < span class = "p" > :< / span > < span class = "w" > < / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "na" > entrySet< / span > < span class = "p" > ())< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-10-64" name = "__codelineno-10-64" href = "#__codelineno-10-64" > < / a > < span class = "w" > < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > Integer< / span > < span class = "o" > > < / span > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "k" > new< / span > < span class = "w" > < / span > < span class = "n" > ArrayList< / span > < span class = "o" > < > < / span > < span class = "p" > ();< / span >
< a id = "__codelineno-10-65" name = "__codelineno-10-65" href = "#__codelineno-10-65" > < / a > < span class = "w" > < / span > < span class = "k" > for< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > Vertex< / span > < span class = "w" > < / span > < span class = "n" > vertex< / span > < span class = "w" > < / span > < span class = "p" > :< / span > < span class = "w" > < / span > < span class = "n" > entry< / span > < span class = "p" > .< / span > < span class = "na" > getValue< / span > < span class = "p" > ())< / span >
< a id = "__codelineno-10-66" name = "__codelineno-10-66" href = "#__codelineno-10-66" > < / a > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "p" > .< / span > < span class = "na" > add< / span > < span class = "p" > (< / span > < span class = "n" > vertex< / span > < span class = "p" > .< / span > < span class = "na" > val< / span > < span class = "p" > );< / span >
< a id = "__codelineno-10-67" name = "__codelineno-10-67" href = "#__codelineno-10-67" > < / a > < span class = "w" > < / span > < span class = "n" > System< / span > < span class = "p" > .< / span > < span class = "na" > out< / span > < span class = "p" > .< / span > < span class = "na" > println< / span > < span class = "p" > (< / span > < span class = "n" > entry< / span > < span class = "p" > .< / span > < span class = "na" > getKey< / span > < span class = "p" > ().< / span > < span class = "na" > val< / span > < span class = "w" > < / span > < span class = "o" > +< / span > < span class = "w" > < / span > < span class = "s" > " : " < / span > < span class = "w" > < / span > < span class = "o" > +< / span > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "w" > < / span > < span class = "o" > +< / span > < span class = "w" > < / span > < span class = "s" > " ," < / span > < span class = "p" > );< / span >
< a id = "__codelineno-10-68" name = "__codelineno-10-68" href = "#__codelineno-10-68" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-10-69" name = "__codelineno-10-69" href = "#__codelineno-10-69" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-10-70" name = "__codelineno-10-70" href = "#__codelineno-10-70" > < / a > < span class = "p" > }< / span >
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
@ -2610,7 +2706,87 @@
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
< div class = "highlight" > < span class = "filename" > graph_adjacency_list.cs< / span > < pre > < span > < / span > < code > < a id = "__codelineno-17-1" name = "__codelineno-17-1" href = "#__codelineno-17-1" > < / a > < span class = "na" > [class]< / span > < span class = "p" > {< / span > < span class = "n" > GraphAdjList< / span > < span class = "p" > }< / span > < span class = "o" > -< / span > < span class = "p" > [< / span > < span class = "n" > func< / span > < span class = "p" > ]{}< / span >
< div class = "highlight" > < span class = "filename" > graph_adjacency_list.cs< / span > < pre > < span > < / span > < code > < a id = "__codelineno-17-1" name = "__codelineno-17-1" href = "#__codelineno-17-1" > < / a > < span class = "cm" > /* 基于邻接表实现的无向图类 */< / span >
< a id = "__codelineno-17-2" name = "__codelineno-17-2" href = "#__codelineno-17-2" > < / a > < span class = "k" > class< / span > < span class = "w" > < / span > < span class = "nc" > GraphAdjList< / span >
< a id = "__codelineno-17-3" name = "__codelineno-17-3" href = "#__codelineno-17-3" > < / a > < span class = "p" > {< / span >
< a id = "__codelineno-17-4" name = "__codelineno-17-4" href = "#__codelineno-17-4" > < / a > < span class = "w" > < / span > < span class = "c1" > // 邻接表, key: 顶点, value: 该顶点的所有邻接结点< / span >
< a id = "__codelineno-17-5" name = "__codelineno-17-5" href = "#__codelineno-17-5" > < / a > < span class = "w" > < / span > < span class = "n" > Dictionary< / span > < span class = "o" > < < / span > < span class = "n" > Vertex< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > Vertex< / span > < span class = "o" > > > < / span > < span class = "w" > < / span > < span class = "n" > adjList< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-6" name = "__codelineno-17-6" href = "#__codelineno-17-6" > < / a >
< a id = "__codelineno-17-7" name = "__codelineno-17-7" href = "#__codelineno-17-7" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 构造函数 */< / span >
< a id = "__codelineno-17-8" name = "__codelineno-17-8" href = "#__codelineno-17-8" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "nf" > GraphAdjList< / span > < span class = "p" > (< / span > < span class = "n" > Vertex< / span > < span class = "p" > [][]< / span > < span class = "w" > < / span > < span class = "n" > edges< / span > < span class = "p" > )< / span >
< a id = "__codelineno-17-9" name = "__codelineno-17-9" href = "#__codelineno-17-9" > < / a > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-10" name = "__codelineno-17-10" href = "#__codelineno-17-10" > < / a > < span class = "w" > < / span > < span class = "k" > this< / span > < span class = "p" > .< / span > < span class = "n" > adjList< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "k" > new< / span > < span class = "w" > < / span > < span class = "n" > Dictionary< / span > < span class = "o" > < < / span > < span class = "n" > Vertex< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > Vertex< / span > < span class = "o" > > > < / span > < span class = "p" > ();< / span >
< a id = "__codelineno-17-11" name = "__codelineno-17-11" href = "#__codelineno-17-11" > < / a > < span class = "w" > < / span > < span class = "c1" > // 添加所有顶点和边< / span >
< a id = "__codelineno-17-12" name = "__codelineno-17-12" href = "#__codelineno-17-12" > < / a > < span class = "w" > < / span > < span class = "k" > foreach< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > Vertex< / span > < span class = "p" > []< / span > < span class = "w" > < / span > < span class = "n" > edge< / span > < span class = "w" > < / span > < span class = "k" > in< / span > < span class = "w" > < / span > < span class = "n" > edges< / span > < span class = "p" > )< / span >
< a id = "__codelineno-17-13" name = "__codelineno-17-13" href = "#__codelineno-17-13" > < / a > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-14" name = "__codelineno-17-14" href = "#__codelineno-17-14" > < / a > < span class = "w" > < / span > < span class = "n" > addVertex< / span > < span class = "p" > (< / span > < span class = "n" > edge< / span > < span class = "p" > [< / span > < span class = "m" > 0< / span > < span class = "p" > ]);< / span >
< a id = "__codelineno-17-15" name = "__codelineno-17-15" href = "#__codelineno-17-15" > < / a > < span class = "w" > < / span > < span class = "n" > addVertex< / span > < span class = "p" > (< / span > < span class = "n" > edge< / span > < span class = "p" > [< / span > < span class = "m" > 1< / span > < span class = "p" > ]);< / span >
< a id = "__codelineno-17-16" name = "__codelineno-17-16" href = "#__codelineno-17-16" > < / a > < span class = "w" > < / span > < span class = "n" > addEdge< / span > < span class = "p" > (< / span > < span class = "n" > edge< / span > < span class = "p" > [< / span > < span class = "m" > 0< / span > < span class = "p" > ],< / span > < span class = "w" > < / span > < span class = "n" > edge< / span > < span class = "p" > [< / span > < span class = "m" > 1< / span > < span class = "p" > ]);< / span >
< a id = "__codelineno-17-17" name = "__codelineno-17-17" href = "#__codelineno-17-17" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-18" name = "__codelineno-17-18" href = "#__codelineno-17-18" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-19" name = "__codelineno-17-19" href = "#__codelineno-17-19" > < / a >
< a id = "__codelineno-17-20" name = "__codelineno-17-20" href = "#__codelineno-17-20" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 获取顶点数量 */< / span >
< a id = "__codelineno-17-21" name = "__codelineno-17-21" href = "#__codelineno-17-21" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "nf" > size< / span > < span class = "p" > ()< / span >
< a id = "__codelineno-17-22" name = "__codelineno-17-22" href = "#__codelineno-17-22" > < / a > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-23" name = "__codelineno-17-23" href = "#__codelineno-17-23" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "w" > < / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "n" > Count< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-24" name = "__codelineno-17-24" href = "#__codelineno-17-24" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-25" name = "__codelineno-17-25" href = "#__codelineno-17-25" > < / a >
< a id = "__codelineno-17-26" name = "__codelineno-17-26" href = "#__codelineno-17-26" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 添加边 */< / span >
< a id = "__codelineno-17-27" name = "__codelineno-17-27" href = "#__codelineno-17-27" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "k" > void< / span > < span class = "w" > < / span > < span class = "nf" > addEdge< / span > < span class = "p" > (< / span > < span class = "n" > Vertex< / span > < span class = "w" > < / span > < span class = "n" > vet1< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > Vertex< / span > < span class = "w" > < / span > < span class = "n" > vet2< / span > < span class = "p" > )< / span >
< a id = "__codelineno-17-28" name = "__codelineno-17-28" href = "#__codelineno-17-28" > < / a > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-29" name = "__codelineno-17-29" href = "#__codelineno-17-29" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "o" > !< / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "n" > ContainsKey< / span > < span class = "p" > (< / span > < span class = "n" > vet1< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "o" > ||< / span > < span class = "w" > < / span > < span class = "o" > !< / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "n" > ContainsKey< / span > < span class = "p" > (< / span > < span class = "n" > vet2< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "o" > ||< / span > < span class = "w" > < / span > < span class = "n" > vet1< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "n" > vet2< / span > < span class = "p" > )< / span >
< a id = "__codelineno-17-30" name = "__codelineno-17-30" href = "#__codelineno-17-30" > < / a > < span class = "w" > < / span > < span class = "k" > throw< / span > < span class = "w" > < / span > < span class = "k" > new< / span > < span class = "w" > < / span > < span class = "nf" > InvalidOperationException< / span > < span class = "p" > ();< / span >
< a id = "__codelineno-17-31" name = "__codelineno-17-31" href = "#__codelineno-17-31" > < / a > < span class = "w" > < / span > < span class = "c1" > // 添加边 vet1 - vet2< / span >
< a id = "__codelineno-17-32" name = "__codelineno-17-32" href = "#__codelineno-17-32" > < / a > < span class = "w" > < / span > < span class = "n" > adjList< / span > < span class = "p" > [< / span > < span class = "n" > vet1< / span > < span class = "p" > ].< / span > < span class = "n" > Add< / span > < span class = "p" > (< / span > < span class = "n" > vet2< / span > < span class = "p" > );< / span >
< a id = "__codelineno-17-33" name = "__codelineno-17-33" href = "#__codelineno-17-33" > < / a > < span class = "w" > < / span > < span class = "n" > adjList< / span > < span class = "p" > [< / span > < span class = "n" > vet2< / span > < span class = "p" > ].< / span > < span class = "n" > Add< / span > < span class = "p" > (< / span > < span class = "n" > vet1< / span > < span class = "p" > );< / span >
< a id = "__codelineno-17-34" name = "__codelineno-17-34" href = "#__codelineno-17-34" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-35" name = "__codelineno-17-35" href = "#__codelineno-17-35" > < / a >
< a id = "__codelineno-17-36" name = "__codelineno-17-36" href = "#__codelineno-17-36" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 删除边 */< / span >
< a id = "__codelineno-17-37" name = "__codelineno-17-37" href = "#__codelineno-17-37" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "k" > void< / span > < span class = "w" > < / span > < span class = "nf" > removeEdge< / span > < span class = "p" > (< / span > < span class = "n" > Vertex< / span > < span class = "w" > < / span > < span class = "n" > vet1< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > Vertex< / span > < span class = "w" > < / span > < span class = "n" > vet2< / span > < span class = "p" > )< / span >
< a id = "__codelineno-17-38" name = "__codelineno-17-38" href = "#__codelineno-17-38" > < / a > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-39" name = "__codelineno-17-39" href = "#__codelineno-17-39" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "o" > !< / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "n" > ContainsKey< / span > < span class = "p" > (< / span > < span class = "n" > vet1< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "o" > ||< / span > < span class = "w" > < / span > < span class = "o" > !< / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "n" > ContainsKey< / span > < span class = "p" > (< / span > < span class = "n" > vet2< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "o" > ||< / span > < span class = "w" > < / span > < span class = "n" > vet1< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "n" > vet2< / span > < span class = "p" > )< / span >
< a id = "__codelineno-17-40" name = "__codelineno-17-40" href = "#__codelineno-17-40" > < / a > < span class = "w" > < / span > < span class = "k" > throw< / span > < span class = "w" > < / span > < span class = "k" > new< / span > < span class = "w" > < / span > < span class = "nf" > InvalidOperationException< / span > < span class = "p" > ();< / span >
< a id = "__codelineno-17-41" name = "__codelineno-17-41" href = "#__codelineno-17-41" > < / a > < span class = "w" > < / span > < span class = "c1" > // 删除边 vet1 - vet2< / span >
< a id = "__codelineno-17-42" name = "__codelineno-17-42" href = "#__codelineno-17-42" > < / a > < span class = "w" > < / span > < span class = "n" > adjList< / span > < span class = "p" > [< / span > < span class = "n" > vet1< / span > < span class = "p" > ].< / span > < span class = "n" > Remove< / span > < span class = "p" > (< / span > < span class = "n" > vet2< / span > < span class = "p" > );< / span >
< a id = "__codelineno-17-43" name = "__codelineno-17-43" href = "#__codelineno-17-43" > < / a > < span class = "w" > < / span > < span class = "n" > adjList< / span > < span class = "p" > [< / span > < span class = "n" > vet2< / span > < span class = "p" > ].< / span > < span class = "n" > Remove< / span > < span class = "p" > (< / span > < span class = "n" > vet1< / span > < span class = "p" > );< / span >
< a id = "__codelineno-17-44" name = "__codelineno-17-44" href = "#__codelineno-17-44" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-45" name = "__codelineno-17-45" href = "#__codelineno-17-45" > < / a >
< a id = "__codelineno-17-46" name = "__codelineno-17-46" href = "#__codelineno-17-46" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 添加顶点 */< / span >
< a id = "__codelineno-17-47" name = "__codelineno-17-47" href = "#__codelineno-17-47" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "k" > void< / span > < span class = "w" > < / span > < span class = "nf" > addVertex< / span > < span class = "p" > (< / span > < span class = "n" > Vertex< / span > < span class = "w" > < / span > < span class = "n" > vet< / span > < span class = "p" > )< / span >
< a id = "__codelineno-17-48" name = "__codelineno-17-48" href = "#__codelineno-17-48" > < / a > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-49" name = "__codelineno-17-49" href = "#__codelineno-17-49" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "n" > ContainsKey< / span > < span class = "p" > (< / span > < span class = "n" > vet< / span > < span class = "p" > ))< / span >
< a id = "__codelineno-17-50" name = "__codelineno-17-50" href = "#__codelineno-17-50" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-51" name = "__codelineno-17-51" href = "#__codelineno-17-51" > < / a > < span class = "w" > < / span > < span class = "c1" > // 在邻接表中添加一个新链表< / span >
< a id = "__codelineno-17-52" name = "__codelineno-17-52" href = "#__codelineno-17-52" > < / a > < span class = "w" > < / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "n" > Add< / span > < span class = "p" > (< / span > < span class = "n" > vet< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "k" > new< / span > < span class = "w" > < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > Vertex< / span > < span class = "o" > > < / span > < span class = "p" > ());< / span >
< a id = "__codelineno-17-53" name = "__codelineno-17-53" href = "#__codelineno-17-53" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-54" name = "__codelineno-17-54" href = "#__codelineno-17-54" > < / a >
< a id = "__codelineno-17-55" name = "__codelineno-17-55" href = "#__codelineno-17-55" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 删除顶点 */< / span >
< a id = "__codelineno-17-56" name = "__codelineno-17-56" href = "#__codelineno-17-56" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "k" > void< / span > < span class = "w" > < / span > < span class = "nf" > removeVertex< / span > < span class = "p" > (< / span > < span class = "n" > Vertex< / span > < span class = "w" > < / span > < span class = "n" > vet< / span > < span class = "p" > )< / span >
< a id = "__codelineno-17-57" name = "__codelineno-17-57" href = "#__codelineno-17-57" > < / a > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-58" name = "__codelineno-17-58" href = "#__codelineno-17-58" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "o" > !< / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "n" > ContainsKey< / span > < span class = "p" > (< / span > < span class = "n" > vet< / span > < span class = "p" > ))< / span >
< a id = "__codelineno-17-59" name = "__codelineno-17-59" href = "#__codelineno-17-59" > < / a > < span class = "w" > < / span > < span class = "k" > throw< / span > < span class = "w" > < / span > < span class = "k" > new< / span > < span class = "w" > < / span > < span class = "nf" > InvalidOperationException< / span > < span class = "p" > ();< / span >
< a id = "__codelineno-17-60" name = "__codelineno-17-60" href = "#__codelineno-17-60" > < / a > < span class = "w" > < / span > < span class = "c1" > // 在邻接表中删除顶点 vet 对应的链表< / span >
< a id = "__codelineno-17-61" name = "__codelineno-17-61" href = "#__codelineno-17-61" > < / a > < span class = "w" > < / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "n" > Remove< / span > < span class = "p" > (< / span > < span class = "n" > vet< / span > < span class = "p" > );< / span >
< a id = "__codelineno-17-62" name = "__codelineno-17-62" href = "#__codelineno-17-62" > < / a > < span class = "w" > < / span > < span class = "c1" > // 遍历其它顶点的链表,删除所有包含 vet 的边< / span >
< a id = "__codelineno-17-63" name = "__codelineno-17-63" href = "#__codelineno-17-63" > < / a > < span class = "w" > < / span > < span class = "k" > foreach< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > Vertex< / span > < span class = "o" > > < / span > < span class = "w" > < / span > < span class = "n" > list< / span > < span class = "w" > < / span > < span class = "k" > in< / span > < span class = "w" > < / span > < span class = "n" > adjList< / span > < span class = "p" > .< / span > < span class = "n" > Values< / span > < span class = "p" > )< / span >
< a id = "__codelineno-17-64" name = "__codelineno-17-64" href = "#__codelineno-17-64" > < / a > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-65" name = "__codelineno-17-65" href = "#__codelineno-17-65" > < / a > < span class = "w" > < / span > < span class = "n" > list< / span > < span class = "p" > .< / span > < span class = "n" > Remove< / span > < span class = "p" > (< / span > < span class = "n" > vet< / span > < span class = "p" > );< / span >
< a id = "__codelineno-17-66" name = "__codelineno-17-66" href = "#__codelineno-17-66" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-67" name = "__codelineno-17-67" href = "#__codelineno-17-67" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-68" name = "__codelineno-17-68" href = "#__codelineno-17-68" > < / a >
< a id = "__codelineno-17-69" name = "__codelineno-17-69" href = "#__codelineno-17-69" > < / a > < span class = "w" > < / span > < span class = "cm" > /* 打印邻接表 */< / span >
< a id = "__codelineno-17-70" name = "__codelineno-17-70" href = "#__codelineno-17-70" > < / a > < span class = "w" > < / span > < span class = "k" > public< / span > < span class = "w" > < / span > < span class = "k" > void< / span > < span class = "w" > < / span > < span class = "nf" > print< / span > < span class = "p" > ()< / span >
< a id = "__codelineno-17-71" name = "__codelineno-17-71" href = "#__codelineno-17-71" > < / a > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-72" name = "__codelineno-17-72" href = "#__codelineno-17-72" > < / a > < span class = "w" > < / span > < span class = "n" > Console< / span > < span class = "p" > .< / span > < span class = "n" > WriteLine< / span > < span class = "p" > (< / span > < span class = "s" > " 邻接表 =" < / span > < span class = "p" > );< / span >
< a id = "__codelineno-17-73" name = "__codelineno-17-73" href = "#__codelineno-17-73" > < / a > < span class = "w" > < / span > < span class = "k" > foreach< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > KeyValuePair< / span > < span class = "o" > < < / span > < span class = "n" > Vertex< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > Vertex< / span > < span class = "o" > > > < / span > < span class = "w" > < / span > < span class = "n" > entry< / span > < span class = "w" > < / span > < span class = "k" > in< / span > < span class = "w" > < / span > < span class = "n" > adjList< / span > < span class = "p" > )< / span >
< a id = "__codelineno-17-74" name = "__codelineno-17-74" href = "#__codelineno-17-74" > < / a > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-75" name = "__codelineno-17-75" href = "#__codelineno-17-75" > < / a > < span class = "w" > < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "kt" > int< / span > < span class = "o" > > < / span > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "k" > new< / span > < span class = "w" > < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "kt" > int< / span > < span class = "o" > > < / span > < span class = "p" > ();< / span >
< a id = "__codelineno-17-76" name = "__codelineno-17-76" href = "#__codelineno-17-76" > < / a > < span class = "w" > < / span > < span class = "k" > foreach< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > Vertex< / span > < span class = "w" > < / span > < span class = "n" > vertex< / span > < span class = "w" > < / span > < span class = "k" > in< / span > < span class = "w" > < / span > < span class = "n" > entry< / span > < span class = "p" > .< / span > < span class = "n" > Value< / span > < span class = "p" > )< / span >
< a id = "__codelineno-17-77" name = "__codelineno-17-77" href = "#__codelineno-17-77" > < / a > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "p" > .< / span > < span class = "n" > Add< / span > < span class = "p" > (< / span > < span class = "n" > vertex< / span > < span class = "p" > .< / span > < span class = "n" > Val< / span > < span class = "p" > );< / span >
< a id = "__codelineno-17-78" name = "__codelineno-17-78" href = "#__codelineno-17-78" > < / a > < span class = "w" > < / span > < span class = "n" > Console< / span > < span class = "p" > .< / span > < span class = "n" > WriteLine< / span > < span class = "p" > (< / span > < span class = "n" > entry< / span > < span class = "p" > .< / span > < span class = "n" > Key< / span > < span class = "p" > .< / span > < span class = "n" > Val< / span > < span class = "w" > < / span > < span class = "o" > +< / span > < span class = "w" > < / span > < span class = "s" > " : [" < / span > < span class = "w" > < / span > < span class = "o" > +< / span > < span class = "w" > < / span > < span class = "kt" > string< / span > < span class = "p" > .< / span > < span class = "n" > Join< / span > < span class = "p" > (< / span > < span class = "s" > " , " < / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "o" > +< / span > < span class = "w" > < / span > < span class = "s" > " ]," < / span > < span class = "p" > );< / span >
< a id = "__codelineno-17-79" name = "__codelineno-17-79" href = "#__codelineno-17-79" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-80" name = "__codelineno-17-80" href = "#__codelineno-17-80" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-81" name = "__codelineno-17-81" href = "#__codelineno-17-81" > < / a > < span class = "p" > }< / span >
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >