Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

Maximum Edge Disjoint Spanning Trees จาก Complete Graph ใน C++

สมมติมี Complete Graph ต้องการนับจำนวน Edge Disjoint Spanning Trees Edge Disjoint Spanning Trees คือ Spanning Trees ที่ไม่มี Edge ไหนซ้ำกันระหว่าง Trees ใน Set สมมติ N=4 Output=2 Complete Graph 4 Vertices:

Maximum Edge Disjoint Spanning Trees จาก Complete Graph ใน C++

2 Edge Disjoint Spanning Trees:

Maximum Edge Disjoint Spanning Trees จาก Complete Graph ใน C++

สูตร: จำนวนสูงสุด = ⌊n/2⌋

ตัวอย่างโค้ด C++

#include <iostream>
#include <cmath>
using namespace std;

int maxEdgeDisjointSpanningTree(int n) {
    return floor(n/2);
}

int main() {
    int n = 4;
    cout << "Maximum Edge Disjoint Spanning Tree: " << maxEdgeDisjointSpanningTree(n);
}

ผลลัพธ์

Maximum Edge Disjoint Spanning Tree: 2