非連結の入力は常に拒否すべきですか?
アプリケーションが単一の連結構造を要求する場合だけです。森を返すなら非連結の成分も有効な場合があります。入力にない接続をアシスタントが強制しないよう、修正依頼の前に区別を明記してください。
def spanning_forest(count, edges):
if type(count) is not int or not 0 <= count <= 64 or type(edges) is not list or len(edges) > 1024:
raise ValueError("forest.input")
ordered = []
for edge in edges:
if type(edge) not in (list, tuple) or len(edge) != 3 or any(type(x) is not int for x in edge):
raise ValueError("forest.edge")
a, b, weight = edge
if not 0 <= a < count or not 0 <= b < count or abs(weight) > 10**9:
raise ValueError("forest.bounds")
ordered.append((weight, min(a, b), max(a, b)))
parent = list(range(count))
def root(node):
while parent[node] != node:
parent[node] = parent[parent[node]]
node = parent[node]
return node
selected, total = [], 0
for weight, a, b in sorted(ordered):
x, y = root(a), root(b)
if x != y:
parent[x] = y
selected.append((a, b, weight))
total += weight
return total, selected
実際の動作を期待する結果と比較します。検証に失敗した場合は、次の変更を加える前に原因を調べてください。
OpenAI互換またはAnthropicのエンドポイントを、ベースURLと必要なAPIキーで設定し、接続をテストします。ローカルのモデルサーバーは起動が必要です。リモートツールは別途データを送信する場合があります。
非連結ノード、多重辺、自己ループ、同点の決定的選択を試し、不正端点や真偽値を入力を変えず拒否してください。
def spanning_forest(count, edges):
if type(count) is not int or not 0 <= count <= 64 or type(edges) is not list or len(edges) > 1024:
raise ValueError("forest.input")
ordered = []
for edge in edges:
if type(edge) not in (list, tuple) or len(edge) != 3 or any(type(x) is not int for x in edge):
raise ValueError("forest.edge")
a, b, weight = edge
if not 0 <= a < count or not 0 <= b < count or abs(weight) > 10**9:
raise ValueError("forest.bounds")
ordered.append((weight, min(a, b), max(a, b)))
parent = list(range(count))
def root(node):
while parent[node] != node:
parent[node] = parent[parent[node]]
node = parent[node]
return node
selected, total = [], 0
for weight, a, b in sorted(ordered):
x, y = root(a), root(b)
if x != y:
parent[x] = y
selected.append((a, b, weight))
total += weight
return total, selected
Forellinは、グラフ処理コードを利用するアプリケーションと一緒に調べるためのWindows向けAIコーディング環境です。入力表現と呼び出し側に必要な結果を示して会話を始めてください。変更を提案する前に、接続の欠落、辺の重複、出力順序の扱いをアシスタントに確認してもらいます。これらの判断も中心となる計算と同様に機能を形作ります。
spanning_forest.pyの文脈では、すべてを接続する試みの失敗ではなく、非連結のノードが想定内である作業を扱えます。どの辺が選ばれ、合計がそれらとどう関係するかをモデルに説明してもらえます。手で確認できる小さなグラフと照らして回答をレビューし、結果の仕様や検証に調整が必要なら絞り込んだ修正を依頼してください。
連結と非連結の入力でプロジェクトの検証を行い、孤立ノードを周辺のアプリケーションがどう表現するか調べます。複数の有効な結果がある場合は、決定的な選択が利用側に必要か合意してください。Forellinは対応するOpenAI互換またはAnthropicのエンドポイントを利用でき、必要なら認証情報を使います。提供されればフィルターなしAIモデルを選べますが、制限、ツール、コンテキスト容量は設定したサービスに依存します。環境は調査を支援し、グラフの例とテスト結果がコード採用の根拠になります。
アプリケーションが単一の連結構造を要求する場合だけです。森を返すなら非連結の成分も有効な場合があります。入力にない接続をアシスタントが強制しないよう、修正依頼の前に区別を明記してください。
任意の有効な最適解でよいか、利用側に再現可能な選択が必要か決めてください。順序や同点処理が重要なら、偶然の入力順に頼らず期待結果に含めて明示的にテストします。
小さな例で、選択された辺と重みの合計を独立に比較してください。合計だけが必要か、選択された構造も必要かを呼び出し側で調べます。両者は異なるアプリケーション動作を支えます。
入力に含まれていても、辺だけの結果からは消える場合があります。周辺の表現を確認し、選択された辺がすべての頂点を表すと思い込まず、保持や表示の方法を決めてください。
Forellin · 更新日 2026-10-25