캡슐화된 체인 포워드 스타 구현

체인 포워드 스타 클래스 (캡슐화 버전)

struct ChainForwardStar {
    vector<int> head, to, next, weight;
    int edgeCount = 0;
    
    ChainForwardStar(int capacity) {
        head.assign(capacity + 1, -1);
        to.resize(capacity + 1);
        next.resize(capacity + 1);
        weight.resize(capacity + 1);
    }
    
    void connect(int from, int dest, int cost = 0) {
        to[edgeCount] = dest;
        weight[edgeCount] = cost;
        next[edgeCount] = head[from];
        head[from] = edgeCount++;
    }
    
    int getHead(int node) const {
        return head[node];
    }
    
    int getDestination(int edgeIndex) const {
        return to[edgeIndex];
    }
    
    int operator[](int edgeIndex) const {
        assert(edgeIndex != -1);
        return weight[edgeIndex];
    }
    
    void moveToNext(int& edgeIndex) {
        edgeIndex = next[edgeIndex];
    }
};

인터페이스 및 멤버 함수

  • ChainForwardStar(int capacity)
    체인 포워드 스타 구조를 초기화합니다. capacity는 최대 간선 수입니다.
  • void connect(int from, int dest, int cost = 0)
    간선을 추가합니다. from은 시작 노드, dest는 도착 노드, cost는 가중치(선택사항)입니다.
  • int getHead(int node)
    node의 연결 리스트 헤드를 반환합니다.
  • int getDestination(int edgeIndex)
    edgeIndex에 해당하는 간선의 목적지를 반환합니다.
  • int operator[](int edgeIndex)
    edgeIndex에 해당하는 간선의 가중치를 반환합니다.
  • void moveToNext(int& edgeIndex)
    edgeIndex를 다음 간선으로 이동시킵니다.

사용 예제:

signed main() {
    ChainForwardStar graph(100);
    graph.connect(1, 2);
    graph.connect(2, 3);
    graph.connect(1, 3, 5);
    graph.connect(1, 4);
    
    int currentEdge = graph.getHead(1);
    while(currentEdge != -1) {
        cout << graph.getDestination(currentEdge) << " " 
             << graph[currentEdge] << endl;
        graph.moveToNext(currentEdge);
    }
}

VS Code 스니펫 설정

다음 내용을 설정 파일에 추가하면 코드 스니펫을 사용할 수 있습니다.

트리거 단어: chainstar

"체인 포워드 스타(캡슐화)": {
  "prefix": "chainstar",
  "body": [
    "struct ChainForwardStar {",
    "    vector<int> head, to, next, weight;",
    "    int edgeCount = 0;",
    "    ",
    "    ChainForwardStar(int capacity) {",
    "        head.assign(capacity + 1, -1);",
    "        to.resize(capacity + 1);",
    "        next.resize(capacity + 1);",
    "        weight.resize(capacity + 1);",
    "    }",
    "    ",
    "    void connect(int from, int dest, int cost = 0) {",
    "        to[edgeCount] = dest;",
    "        weight[edgeCount] = cost;",
    "        next[edgeCount] = head[from];",
    "        head[from] = edgeCount++;",
    "    }",
    "    ",
    "    int getHead(int node) const {",
    "        return head[node];",
    "    }",
    "    ",
    "    int getDestination(int edgeIndex) const {",
    "        return to[edgeIndex];",
    "    }",
    "    ",
    "    int operator[](int edgeIndex) const {",
    "        assert(edgeIndex != -1);",
    "        return weight[edgeIndex];",
    "    }",
    "    ",
    "    void moveToNext(int& edgeIndex) {",
    "        edgeIndex = next[edgeIndex];",
    "    }",
    "};"
  ],
  "description": "체인 포워드 스타 캡슐화 구현"
}

태그: graph-theory data-structure cpp algorithm

7월 4일 17:41에 게시됨