(Graph Theory) Proving Prim‘s Algorithm

The Algorithm

Notice that this algorithm only applies to connected graphs.

  • Start with a blank list v i s i t e d : = { } visited:=\{\} visited:={}, which is used to record visted vertices. We also need to keep track of the edges we pick.

  • The first iteration starts here: Pick some arbitrary vertex v 1 v_1 v1 to start, update the list to { v 1 } \{v_1\} {v1}. This iteration, the 0 0 0th iteration, ends.

  • If v 1 v_1 v1 is not all the vertex we have in the graph, continue, or the algorithm ends.

  • The second iteration starts. Look for the edge incident on v 1 v_1 v1 such that

    • (a) the edge is incident on v 1 v_1 v1 and an unvisited vertex
    • (b) the edge has the smallest weight
  • Say the edge we pick, e 1 e_1 e1, is incident on v 1 v_1 v1 and v 2 v_2 v2.
    - Update the list to v i s i t e d = { v 1 , v 2 } visited=\{v_1,v_2\} visited={v1,v2} and record e 1 e_1 e1. This iteration is finished. If v 1 , v 2 v_1,v_2 v1,v2 are not all the vertices we have in the graph, continue, or the algorithm ends.

  • The next iteration will be to choose the edge with the smallest weight among all the edges incident on v 1 v_1 v1 OR v 2 v_2 v2 (but not both, or we will be choosing a duplicate)and some unvisited vertex. Say we choose an edge, e 2 e_2 e2, that is incident on v 3 v_3 v3.

  • Update the list to v i s i t i e d = { v 1 , v 2 , v 3 } visitied = \{v_1,v_2,v_3\} visitied={v1,v2,v3} and record e 2 e_2 e2. This iteration is finished.

  • Repeat this process again, but this time we will be choosing the edge with the smallest weight among all the edges incident on v 1 v_1 v1 OR v 2 v_2 v2 OR v 3 v_3 v3 and some univisited vertex.

  • When the last vertex in the graph is added to v i s i t e d visited visited, we are done.

Prim’s Algorithm is Greedy

  • Prim’s Algorithm is a greedy algorithm, which means in each iteration, it always makes the locally best choice (i.e. the best choice available in each iteration). However, we might argue that a collection of local optimum may not always give the global optima, yet the algorithm does always give the minimum spanning tree. We want to prove this.

Prim’s Algorithm Always Works

  • Claim: Prim’s Algorithm is correct. That is, the tree obtained at the termination of the algorithm is a minimum spanning tree.

Proof

  • Assume T i T_i Ti is the (sub)graph obtained after the i i ith iteration. We will be showing, by induction, that for each i , 0 ≤ i ≤ n − 1 i,0≤i≤n-1 i,0in1 (assuming n n n vertices), T i T_i Ti is in a minimum spanning tree.
  • Let T 0 T_0 T0 be the graph obtained after ther 0 0 0th iteration. Notice that T 0 T_0 T0 must be contained in every minimum spanning tree because a spanning tree contains all vertices, and T 0 T_0 T0 is just a single vertex.
  • Assume for i = k , k ≥ 0 i=k,k≥0 i=k,k0, T i T_i Ti is contained in some minimum spanning tree.
  • Consider i = k + 1 i=k+1 i=k+1. The algorithm will pick an edge incident on some vertex v ∈ T k v\in T_{k} vTk and another vertex w ∉ T k w\not\in T_k wTk, say e e e, with minimum weight, to add to T k T_k Tk (along with w w w) to form T k + 1 T_{k+1} Tk+1. Assume the minimum spanninbg tree that T k T_k Tk is in is T 1 T_1 T1.
  • If T 1 T_1 T1 contains e e e, then by construction the only way it contains e e e is by having T k + 1 T_{k+1} Tk+1 as its subgraph, then we are done.
  • If T 1 T_1 T1 does not contain e e e, then T 2 : = T 1 ∪ { e } T_2:=T_1\cup \{e\} T2:=T1{e} contains a cycle (as T 1 T_1 T1 is already a spanning tree). Assume we remove from T 2 T_2 T2 an edge that is not e e e, say e ′ e' e incident on x ∈ T k x\in T_{k} xTk and y ∉ T k y\not\in T_{k} yTk, from the cycle, and obtain T 3 T_3 T3 i.e. T 3 = T 1 ∪ { e } − { e ′ } T_3=T_1\cup \{e\}-\{e'\} T3=T1{e}{e}.
  • Notice we only removed this single edge, so other edges and vertices in T 1 T_1 T1 are intact. Therefore, By the important exercise, we know T 3 T_3 T3 is still connected, and it is acyclic.
  • Moreover, as T 3 T_3 T3 contains all the vertices, it is a spanning tree. Since T 1 T_1 T1 is already a minimum spanning tree, w ( T 1 ) ≤ w ( T 3 ) w(T_1)≤w(T_3) w(T1)w(T3).
  • However, because e e e is the edge chosen by the algorithm at the ( k + 1 ) (k+1) (k+1)th iteration, w ( e ) ≤ w ( e ′ ) w(e)≤w(e') w(e)w(e).
  • Thus, w ( T 3 ) = w ( T 1 ) + w ( e ) − w ( e ′ ) ≤ w ( T 1 ) w(T_3)=w(T_1)+w(e)-w(e')≤w(T_1) w(T3)=w(T1)+w(e)w(e)w(T1).
  • Thus, w ( T 3 ) = w ( T 1 ) w(T_3)=w(T_1) w(T3)=w(T1), which means T k + 1 T_{k+1} Tk+1 is in T 3 T_3 T3, another minimum spanning tree.
  • Thus, T k + 1 T_{k+1} Tk+1 is always in some minimum spanning tree, which verifies the claim.
    Notice that in the proof we used an important technique regarding spanning tree: if you add any edge that was not originally in the spanning tree to a spanning tree, you will end up with a cycle in the resulting graph. Then, you may want to use the important exercise to show something about connectivity. Below is another application of this idea.

The Edge with Minimum Weight

  • Claim: The edge with the minimum weight in a connected graph is in every minimum spanning tree of the graph.

Proof

  • Suppose e e e in a connected graph G G G is the edge with the minimun weight.
  • Suppose T T T is a minimum spanning tree of G G G. Suppose it does not contain e e e. Then we add e e e to T T T, and we obtain a cycle containing e e e. Suppose we remove a single edge in that cycle that is not e e e. Say we remove e ′ e' e. By the important exercise, we will have another spanning tree. However, since w ( e ) ≤ w ( e ′ ) w(e)≤w(e') w(e)w(e), the resulting spanning tree’s weight is less than or equal to T T T. If w ( e ) = w ( e ′ ) w(e)=w(e') w(e)=w(e), then both e e e and e ′ e' e are the edges with the minimum weight, and we are done; if w ( e ) < w ( e ′ ) w(e)<w(e') w(e)<w(e), we get a contradiction, so T T T must contain e e e in the first place.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值