從給定的子序列構建原始陣列
任務是從一組給定的子序列中重建初始叢集。這包括根據給定的子序列找到元件在唯一叢集中出現的順序。透過分析子序列中元件之間的設計和連線,計算決定元件的正確排列並重建初始叢集。重建的叢集表示推斷子序列的初始序列。此準備工作使我們能夠恢復初始叢集結構和資料,從而能夠分析或操作資料。
使用的方法
深度優先搜尋
拓撲排序
深度優先搜尋
深度優先搜尋(DFS)可用於從給定的子序列中重建初始叢集。在這種情況下,DFS 涉及從第一個元件開始並遍歷後續元件來遞迴遍歷子序列。透過跟蹤已訪問的元素及其出現,DFS 可以識別原始叢集中元件的正確順序。透過對每個子序列重複此過程,DFS 確保所有元件都被訪問,並且已訪問元件的最終順序表示從給定子序列重建的原始叢集。
演算法
建立一個空列表來儲存重建的原始陣列。
建立一個空集合來跟蹤已訪問的元素。
對每個子序列執行以下步驟
a. 從子序列中的第一個元件開始。
b. 如果該元件尚未訪問
i. 將該元件標記為已訪問。
ii. 將該元件新增到重建的陣列中。
iii. 對子序列中的後續元件遞迴應用 DFS。
返回重建的原始叢集。
示例
#include <iostream>
#include <vector>
#include <unordered_set>
// Function 1: Check if an element exists in the purge set
bool checkInPurgeSet(const std::unordered_set<int>& purgeSet, int element) {
return purgeSet.find(element) != purgeSet.end();
}
// Function 2: Add an element to the purge list and set
void addToPurge(std::vector<int>& purgeList, std::unordered_set<int>& purgeSet, int element) {
purgeList.push_back(element);
purgeSet.insert(element);
}
int main() {
std::vector<int> purgeList;
std::unordered_set<int> purgeSet;
// Function 1: Check if an element exists in the purge set
int element1 = 5;
bool existsInPurgeSet = checkInPurgeSet(purgeSet, element1);
std::cout << "Element " << element1 << " exists in purge set: " << existsInPurgeSet << std::endl;
// Function 2: Add an element to the purge list and set
int element2 = 7;
addToPurge(purgeList, purgeSet, element2);
std::cout << "Purge List: ";
for (int i : purgeList) {
std::cout << i << " ";
}
return 0;
}
輸出
Element 5 exists in purge set: 0 Purge List: 7
拓撲排序
拓撲排序是一種基於圖的演算法,用於確定有向圖中元件的線性順序。在從給定子序列構建初始叢集的上下文中,拓撲排序有助於確定元件出現的正確順序。透過將子序列中的每個元件視為一個節點,並根據其出現建立邊,該演算法構建了一個有向圖。對該圖執行拓撲排序會產生一個線性順序,該順序表示重建的原始叢集,從而能夠根據子序列中元件之間的關係恢復初始序列。
演算法
建立一個空有向圖。
遍歷每個子序列
a. 對於子序列中的每個元件
如果該元件還不是圖中的節點,則為其建立一個新節點。
如果子序列中存在前一個元件,則在圖中從前一個元件到當前元件新增一條有向邊。
對圖執行拓撲排序以獲得元素的線性順序。
初始化一個空棧和一個已訪問集合。
對於圖中的每個節點
如果該節點尚未訪問,則對該節點執行深度優先搜尋(DFS)。
在 DFS 期間,遞迴訪問相鄰節點並將它們新增到棧中。
從棧中彈出元素以獲得元素的線性順序。
此順序表示重建的原始陣列。
示例
#include <iostream>
#include <vector>
#include <stack>
#include <unordered_set>
#include <algorithm>
// Structure to represent a component
struct Component {
int id;
std::vector<int> neighbors;
};
// Function to perform topological sorting
void topologicalSort(const std::vector<Component>& components, int current, std::vector<bool>& visited, std::stack<int>& sorted) {
visited[current] = true;
for (int neighbor : components[current].neighbors) {
if (!visited[neighbor]) {
topologicalSort(components, neighbor, visited, sorted);
}
}
sorted.push(current);
}
// Function to generate the purge-coordinated graph
std::vector<int> generatePurgeCoordinatedGraph(const std::vector<std::vector<int>>& subSequences) {
std::vector<Component> graph;
std::unordered_set<int> hubs;
// Traverse each sub-sequence
for (const auto& subSeq : subSequences) {
int previous = -1;
for (int component : subSeq) {
if (hubs.find(component) == hubs.end()) {
// Create an unused hub for the component
hubs.insert(component);
graph.push_back({ component, {} });
}
if (previous != -1) {
// Include a coordinated edge from previous component to current component
graph[previous].neighbors.push_back(component);
}
previous = component;
}
}
// Perform topological sorting to get a direct arrangement of the elements
std::stack<int> sorted;
std::vector<bool> visited(graph.size(), false);
for (int i = 0; i < graph.size(); ++i) {
if (!visited[i]) {
topologicalSort(graph, i, visited, sorted);
}
}
// Get the direct arrangement of the elements
std::vector<int> arrangement;
while (!sorted.empty()) {
arrangement.push_back(sorted.top());
sorted.pop();
}
return arrangement;
}
int main() {
std::vector<std::vector<int>> subSequences = {
{ 1, 2, 3 },
{ 2, 4 },
{ 3, 5 },
{ 4 },
{ 5 },
{ 6 },
{ 6, 7 }
};
std::vector<int> arrangement = generatePurgeCoordinatedGraph(subSequences);
// Print the direct arrangement of elements
for (int element : arrangement) {
std::cout << element << " ";
}
std::cout << std::endl;
return 0;
}
輸出
6 7 1 2 4 3 5 0
結論
本文對基於一組給定子序列重建初始叢集或叢集的演算法進行了說明和實現。本文討論了兩種實現此任務的策略:深度優先搜尋(DFS)和拓撲排序。它概述了分步方法,併為每種演算法提供了 C 語言的示例程式碼。這些演算法有助於識別子序列內元件的正確順序和關係,從而能夠恢復初始叢集結構和資料。通過了解基本原理並使用這些技術,可以分析或操作從初始陣列中推斷出的資料。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP