Graph

A Graph consists of a finite set of vertices (or nodes) and set of Edges which connect a pair of nodes.

Terms

Mathematical graphs can be represented in data structure. We can represent a graph using an array of vertices and a two-dimensional array of edges. Before we proceed further, let's familiarize ourselves with some important terms

  • Vertex ( Node )

    : Each node of the graph is represented as a vertex. In the following example, the labeled circle represents vertices. Thus, A to G are vertices. We can represent them using an array as shown in the following image. Here A can be identified by index 0. B can be identified using index 1 and so on.

  • Edge

    : represents a path between two vertices or a line between two vertices. In the following example, the lines from A to B, B to C, and so on represents edges. We can use a two-dimensional array to represent an array as shown in the following image. Here AB can be represented as 1 at row 0, column 1, BC as 1 at row 1, column 2 and so on, keeping other combinations as 0.

  • Adjacency

    : Two node or vertices are adjacent if they are connected to each other through an edge. In the following example, B is adjacent to A, C is adjacent to B, and so on.

  • Path

    : Path represents a sequence of edges between the two vertices. In the following example, ABCD represents a path from A to D.

  • Reference terms

    ์•„์ง๊นŒ์ง€ ํ•„์š”ํ•ด๋ณธ์ ์ด ์—†์Œ

    • ์ •์ ์˜ ์ฐจ์ˆ˜ ( Degree ): ๋ฌด๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์—์„œ ํ•˜๋‚˜์˜ ์ •์ ์— ์ธ์ ‘ํ•œ ์ •์ ์˜ ์ˆ˜

    • ์ง„์ž… ์ฐจ์ˆ˜ ( In-Degree ): ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์—์„œ ์™ธ๋ถ€์—์„œ ์˜ค๋Š” ๊ฐ„์„ ์˜ ์ˆ˜

    • ์ง„์ถœ ์ฐจ์ˆ˜ ( Out-Degree ): ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์—์„œ ์™ธ๋ถ€๋กœ ํ–ฅํ•˜๋Š” ๊ฐ„์„ ์˜ ์ˆ˜

    • ๊ฒฝ๋กœ ๊ธธ์ด ( Path Length ): ๊ฒฝ๋กœ๋ฅผ ๊ตฌ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋œ ๊ฐ„์„ ์˜ ์ˆ˜

    • ๋‹จ์ˆœ ๊ฒฝ๋กœ ( Simple Path ): ์ฒ˜์Œ ์ •์ ๊ณผ ๋ ์ •์ ์„ ์ œ์™ธํ•˜๊ณ  ์ค‘๋ณต๋œ ์ •์ ์ด ์—†๋Š” ๊ฒฝ๋กœ

    • ์‚ฌ์ดํด ( Cycle ): ๋‹จ์ˆœ ๊ฒฝ๋กœ์˜ ์‹œ์ž‘ ์ •์ ๊ณผ ์ข…๋ฃŒ ์ •์ ์ด ๋™์ผํ•œ ๊ฒฝ์šฐ

Implementation

'''
Here A can be identified by index 0. B can be identified using index 1 and so on.
'''
graph = [
    [1, 4, 5],
    [0, 2],
    [1, 3],
    [2],
    [0],
    [0, 6],
    [5]
]

Last updated

Was this helpful?