도움말닫기
편집할 때 기술적인 문제가 발생했다면 보고해 주세요.
알림 2개닫기

경고: 로그인하지 않았습니다. 편집을 하면 IP 주소가 공개되게 됩니다. 로그인하거나 계정을 생성하면 편집자가 사용자 이름으로 기록되고, 다른 장점도 있습니다.

이 편집기가 공식적으로 지원하지 않는 브라우저를 사용하고 있습니다.

Generic Greedy Minimum Spanning Tree Algorithm

IT 위키

Generic Greedy Minimum Spanning Tree Algorithm is a fundamental approach for constructing a Minimum Spanning Tree (MST) by iteratively selecting the smallest available edge that does not form a cycle. It is the basis for well-known MST algorithms such as Kruskal’s and Prim’s algorithms.

1 Concept[편집 | 원본 편집]

The generic greedy MST algorithm follows a greedy strategy:

  1. Initialize an empty set to store the MST edges.
  2. Sort all edges by weight (if not already sorted).
  3. Iterate through edges, selecting the smallest edge that does not form a cycle.
  4. Repeat until the MST contains exactly (N - 1) edges, where N is the number of vertices.

This algorithm ensures that the MST is constructed efficiently by always choosing the lowest-cost option available at each step.

2 Algorithm[편집 | 원본 편집]

The generic greedy MST algorithm can be described as:

  1. Initialize an empty set T for the MST.
  2. Sort all edges in non-decreasing order of weight.
  3. For each edge (u, v) in sorted order:
    • If adding (u, v) to T does not form a cycle, include it in the MST.
  4. Repeat until the MST contains (N - 1) edges.

3 Example[편집 | 원본 편집]

Consider the following weighted graph:

Vertex Pair Edge Weight
A - B 4
A - C 3
B - C 1
B - D 2
C - D 5

Applying the generic greedy MST algorithm:

  1. Select B - C (1) (smallest edge).
  2. Select B - D (2) (next smallest).
  3. Select A - C (3) (next smallest, does not form a cycle).
  4. The MST is complete with edges B - C, B - D, A - C.

Total MST weight: 1 + 2 + 3 = 6

4 Implementation[편집 | 원본 편집]

A simple implementation of the generic greedy MST algorithm using Kruskal's approach:

class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n))

    def find(self, u):
        if self.parent[u] != u:
            self.parent[u] = self.find(self.parent[u])
        return self.parent[u]

    def union(self, u, v):
        root_u = self.find(u)
        root_v = self.find(v)
        if root_u != root_v:
            self.parent[root_u] = root_v

def generic_greedy_mst(edges, n):
    edges.sort(key=lambda x: x[2])
    uf = UnionFind(n)
    mst = []
    
    for u, v, weight in edges:
        if uf.find(u) != uf.find(v):
            uf.union(u, v)
            mst.append((u, v, weight))
        if len(mst) == n - 1:
            break

    return mst

edges = [(0, 1, 4), (0, 2, 3), (1, 2, 1), (1, 3, 2), (2, 3, 5)]
mst = generic_greedy_mst(edges, 4)
print("Minimum Spanning Tree:", mst)

5 Properties[편집 | 원본 편집]

  • Greedy Approach
    • Always selects the smallest available edge at each step.
  • Cycle Avoidance
    • Ensures no cycles are formed, maintaining the tree structure.
  • Efficiency
    • Runs in O(E log E) time complexity when implemented with a priority queue or sorting.

6 Applications[편집 | 원본 편집]

  • Network Design
    • Used in designing efficient communication, power, and transportation networks.
  • Approximation Algorithms
    • Forms the basis for solving hard problems like the Traveling Salesman Problem (TSP).
  • Cluster Analysis
    • Used in hierarchical clustering techniques.

7 See Also[편집 | 원본 편집]

Generic Greedy Minimum Spanning Tree Algorithm is a fundamental approach for constructing a Minimum Spanning Tree (MST) by iteratively selecting the smallest available edge that does not form a cycle. It is the basis for well-known MST algorithms such as Kruskal’s and Prim’s algorithms.

Concept

The generic greedy MST algorithm follows a greedy strategy:

  1. Initialize an empty set to store the MST edges.

  2. Sort all edges by weight (if not already sorted).

  3. Iterate through edges, selecting the smallest edge that does not form a cycle.

  4. Repeat until the MST contains exactly (N - 1) edges, where N is the number of vertices.

This algorithm ensures that the MST is constructed efficiently by always choosing the lowest-cost option available at each step.

Algorithm

The generic greedy MST algorithm can be described as:

  1. Initialize an empty set T for the MST.

  2. Sort all edges in non-decreasing order of weight.

  3. For each edge (u, v) in sorted order:

    • If adding (u, v) to T does not form a cycle, include it in the MST.

  4. Repeat until the MST contains (N - 1) edges.

Example

Consider the following weighted graph:

Vertex Pair

Edge Weight

A - B

4

A - C

3

B - C

1

B - D

2

C - D

5

Applying the generic greedy MST algorithm:

  1. Select B - C (1) (smallest edge).

  2. Select B - D (2) (next smallest).

  3. Select A - C (3) (next smallest, does not form a cycle).

  4. The MST is complete with edges B - C, B - D, A - C.

Total MST weight: 1 + 2 + 3 = 6

Implementation

A simple implementation of the generic greedy MST algorithm using Kruskal's approach:

class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n))

    def find(self, u):
        if self.parent[u] != u:
            self.parent[u] = self.find(self.parent[u])
        return self.parent[u]

    def union(self, u, v):
        root_u = self.find(u)
        root_v = self.find(v)
        if root_u != root_v:
            self.parent[root_u] = root_v

def generic_greedy_mst(edges, n):
    edges.sort(key=lambda x: x[2])
    uf = UnionFind(n)
    mst = []
    
    for u, v, weight in edges:
        if uf.find(u) != uf.find(v):
            uf.union(u, v)
            mst.append((u, v, weight))
        if len(mst) == n - 1:
            break

    return mst

edges = [(0, 1, 4), (0, 2, 3), (1, 2, 1), (1, 3, 2), (2, 3, 5)]
mst = generic_greedy_mst(edges, 4)
print("Minimum Spanning Tree:", mst)

Properties

  • Greedy Approach

    • Always selects the smallest available edge at each step.

  • Cycle Avoidance

    • Ensures no cycles are formed, maintaining the tree structure.

  • Efficiency

    • Runs in O(E log E) time complexity when implemented with a priority queue or sorting.

Applications

  • Network Design

    • Used in designing efficient communication, power, and transportation networks.

  • Approximation Algorithms

    • Forms the basis for solving hard problems like the Traveling Salesman Problem (TSP).

  • Cluster Analysis

    • Used in hierarchical clustering techniques.

See Also