아래 튜토리얼은 graph-tool 라이브러리에서 제공하는 vertex_hist() 함수를 이용하여, 그래프의 정점에 대한 다양한 특성(차수나 다른 속성)에 대한 히스토그램을 효율적으로 계산하고 시각화하는 방법을 단계별로 설명합니다. 초보자를 위해 파이썬 코드 예시, 상세한 한글 주석, 개념적 배경을 친절하고 정성스럽게 풀어보겠습니다.
vertex_hist()란?counts, bins = graph_tool.stats.vertex_hist(
g,
deg,
bins=[0, 1],
float_count=True
)"in", "out", "total" 또는 VertexPropertyMap (수치형).[0,1] 형태면, 실제 히스토그램 구간을 자동 설정(간격=1).deg가 가리키는 값(차수 또는 property) 확인 → 적절한 bin에 카운팅.counts: 각 bin별 카운트(기본 float, float_count=False면 int).bins: bin 구간의 끝점 리스트.문서에서 O(V) (정점 수만큼) 시간이 걸린다고 명시. 실제로, 정점을 한 번씩 스캔하면 충분한 간단한 알고리즘.
다음 코드는 임의 그래프를 만든 뒤, “정점의 out-degree”에 대한 히스토그램을 구하고 시각화하는 예시입니다.
# vertex_hist_tutorial.py
import graph_tool.all as gt
import numpy as np
import matplotlib.pyplot as plt
def main():
# 1) 그래프 생성: random_graph()로 예시
# - 정점 1000개, 포아송(lambda=5) 분포 기반 차수
g = gt.random_graph(1000, lambda: np.random.poisson(5),directed=False)
# => 무방향, average degree ~10 근방 기대
print("Number of vertices:", g.num_vertices())
print("Number of edges:", g.num_edges())
# 2) vertex_hist: out-degree(무방향 그래프이므로 out=total 과 동일)
# bins=[0,1] => 자동으로 (max_deg+1)까지 간격 1로
counts, bin_edges = gt.vertex_hist(g, "out", bins=[0,1], float_count=True)
# 3) 결과 확인
print("Counts:", counts)
print("Bin edges:", bin_edges)
# 4) 시각화: 막대그래프
# bin_edges는 bin 개수+1 길이이므로, 막대의 x좌표를 중간값으로 설정
bin_centers = 0.5*(bin_edges[:-1] + bin_edges[1:])
plt.figure(figsize=(8,4))
plt.bar(bin_centers, counts, width=0.8, align='center', alpha=0.7)
plt.title("Histogram of vertex out-degree")
plt.xlabel("Degree")
plt.ylabel("Count")
plt.grid(True)
plt.show()
if __name__=="__main__":
main()
gt.random_graph(1000, lambda: (poisson(5), poisson(5))):vertex_hist(g, "out", bins=[0,1]):[0,1]로 주면, 자동으로 0부터 최대값까지 폭=1인 구간 설정.counts: 각 bin별 수(부동소수).bin_edges: bin 경계값(ex. [0,1,2,...,K]).(bin_edges[i] + bin_edges[i+1])/2plt.bar(...)."in", "out", "total":VertexPropertyMap:[a,b] 길이가 2인 리스트: a, a+b, a+2b, ... 형태 bin 에지 생성True(기본): counts를 float로 반환(확장성).False: 정수 count.아래는 정점 속성(예: vprop = g.new_vertex_property("float"))에 대한 히스토그램 예:
vprop = g.new_vertex_property("float")
# 임의로 값 지정
for v in g.vertices():
vprop[v] = np.random.uniform(0, 100)
counts, bins = gt.vertex_hist(g, vprop, bins=[0,10], float_count=False)
# => 0부터 최대값 까지, 폭=10 구간
counts와 bins를 이용해 바로 matplotlib 막대그래프, 누적분포, 로그스케일 등 원하는 식으로 그림.vertex_hist(): 그래프 정점에 대해, 차수 혹은 임의 속성값의 히스토그램을 쉽게 구할 수 있는 함수.counts, bins = gt.vertex_hist(g, deg="out", bins=[0,1], float_count=True)이상으로 vertex_hist 함수에 대한 자세한 설명과 예시 코드를 마쳤습니다. 도시·교통 등 정점 중심 빅데이터에서 각 노드의 속성(차수·인구·종류 등)의 분포를 쉽게 그려볼 수 있고, 수 분할(히스토그램) 기반의 탐색 분석이 가능해집니다.
아래 튜토리얼은 graph-tool 라이브러리의 edge_hist() 함수를 이용해 그래프에서 특정 간선(edge) 속성의 분포(히스토그램)를 손쉽게 구하고 시각화하는 방법을 설명합니다. 함수 호출 시, 원하는 속성을 bin 단위로 세어, numpy.ndarray 형태의 결과(카운트, bins)로 반환하므로, 간선 속성 분석에 유용합니다.
edge_hist() 개요counts, bins = graph_tool.stats.edge_hist(
g,
eprop,
bins=[0, 1],
float_count=True
)
g: 그래프(Graph).eprop: EdgePropertyMap(실수형 혹은 정수형) — ex) 어떤 간선 가중치, 용량, 길이 등.bins: 히스토그램 구간(“엣지”라곤 하지만 여기서 bin edges 의미). [0,1] → 자동으로 최대값까지 간격=1씩 구간.[0,0.1,0.2,0.3, ...] 등 임의 리스트로 세밀하게 지정 가능.[0, 1]), 내부적으로 “최댓값”까지 step=1씩 bins 생성.float_count: bin에 대한 카운트가 float(True) 또는 int(False)로 반환 여부.반환:
counts: 각 bin별 카운트(기본 float)bins: bin 경계값(길이 = bin 개수+1)문서에 따르면, 시간복잡도는 (O(E)). 즉, 간선 수만큼 한 번씩 속성 읽고 bin 분류.
아래 예시는 무작위 그래프를 생성하고, 각 간선에 난수(0~1)를 할당한 뒤, 해당 속성의 히스토그램을 그려보는 코드입니다.
# edge_hist_tutorial.py
import graph_tool.all as gt
import numpy as np
import matplotlib.pyplot as plt
def main():
# 1) 무작위 그래프 생성 (1000 노드, 각 노드 차수=5 근사)
# => graph_tool.random_graph()를 사용
g = gt.random_graph(1000, lambda: np.random.poisson(5), directed=False)
print("Number of vertices:", g.num_vertices()) # 1000
print("Number of edges:", g.num_edges()) # 대략 ~2500정도 (정규5 -> average deg=10/2=5?)
# 2) edge property 생성 (double type)
eprop = g.new_edge_property("double")
# 3) 임의 난수(0..1) 할당
arr = eprop.get_array()
arr[:] = np.random.random(g.num_edges())
# 이제 eprop[e] 는 [0..1) 범위 랜덤 값
# 4) edge_hist: bins => 예) 0..1 구간을 10등분
bins_input = np.linspace(0, 1, 11) # [0., 0.1, 0.2,...1.0]
counts, bin_edges = gt.edge_hist(g, eprop, bins=bins_input, float_count=True)
# 5) 결과 확인
print("Counts:", counts)
print("Bin edges:", bin_edges)
# 6) 시각화: 막대그래프
bin_centers = 0.5*(bin_edges[:-1] + bin_edges[1:])
width = bin_edges[1] - bin_edges[0]
plt.figure(figsize=(8,4))
plt.bar(bin_centers, counts, width=width, align='center', alpha=0.7)
plt.title("Histogram of edge property (random 0..1) in the graph")
plt.xlabel("Edge property value")
plt.ylabel("Count")
plt.grid(True)
plt.show()
if __name__ == "__main__":
main()
random_graph(1000, lambda: (5,5))로 1000개 노드를 가진 그래프, 평균 차수 ~5인 무방향 그래프.eprop = g.new_edge_property("double")np.random.random(...)으로 [0,1) 범위.edge_hist(...): bins=np.linspace(0,1,11) => 구간 [0~0.1), [0.1~0.2), ..., [0.9~1.0]counts, bin_edges 반환. plt.bar(...)로 막대그래프 시각화.[a, b] 길이가 2인 리스트: 자동으로 a, a+b, a+2b, ... 형태로 bin edges 생성.np.linspace 등 → bin 경계 수동 지정.[0,5,10,20] => bin 3개: [0..5), [5..10), [10..20).float_count=True/False 차이True(기본): 결과 counts가 float. ex) [ 123. 456. ...]False: int 카운트. ex) [123 456 ...]실질적 차이는 “정밀도”가 아니라, bin count를 부동소수로 합산할 필요가 있을 때(예: 확률 가중 등) 유용.
edge_hist로 히스토그램 => 길이 분포, 속도 분포, 용량 분포 등 분석 가능.road_length = g.new_edge_property("double")
# ... 값 할당 ...
counts, bins = gt.edge_hist(g, road_length, bins=[0,10], float_count=False)
# => 0..최댓값까지 폭=10씩 bin
edge_hist(): 그래프 간선에 대해, 특정 속성의 히스토그램(bin 단위 카운트) 계산counts, bins = gt.edge_hist(g, eprop, bins=[start, step], float_count=True)or 커스텀 bin.이상으로 edge_hist() 함수에 대한 구체적 설명과 예시 코드를 마쳤습니다. 도시·교통 네트워크에서 “도로별 속성(길이, 용량, 혼잡도 등)의 분포”를 빠르고 간단히 파악해보는 데 유용하게 쓸 수 있습니다.
아래 튜토리얼은 graph-tool 라이브러리의 vertex_average() 함수를 사용하여, 그래프의 정점에 대해 특정 속성(차수나 임의 프로퍼티) 의 평균과 표준편차를 효율적으로 계산하고 활용하는 과정을 자세히 설명합니다. 초보자를 위해 파이썬 코드 예시, 상세한 한글 주석, 추가 팁을 포함했습니다.
vertex_average()의 기본 개념avg, std = graph_tool.stats.vertex_average(g, deg)
g: Graph 객체.deg: 문자열 "in", "out", "total" (정점 차수) 혹은 VertexPropertyMap (정점 프로퍼티).avg: float, 해당 속성(혹은 차수)의 전체 정점 평균.std: float, 그 평균의 표준편차(표본 표준편차)."in", "out", "total":"out"="total", "in"="total" 같은 의미.deg가 VertexPropertyMap이라면, 임의의 실수형 값(예: 인구, 수요, 점수 등)을 가질 수 있음.아래는 무방향 예시 그래프를 만들고, "total" 차수에 대해 평균과 표준편차를 구하는 예입니다.
# vertex_average_tutorial.py
import graph_tool.all as gt
import numpy as np
def main():
# 1) 무작위 그래프 생성
# - 500개 노드, 포아송(λ=5) 근사 차수
g = gt.random_graph(500, lambda: (np.random.poisson(5),
np.random.poisson(5)),
directed=False)
print("Number of vertices:", g.num_vertices())
print("Number of edges:", g.num_edges())
# 2) vertex_average: total degree
avg, std = gt.vertex_average(g, deg="total")
print("Average degree (total):", avg)
print("Std dev of degree:", std)
if __name__=="__main__":
main()
gt.random_graph(500, lambda: (poisson(5), poisson(5)), directed=False): vertex_average(g, deg="total"): "total" 차수가 곧 각 노드가 가진 “degree”.차수 대신, 정점 속성을 직접 만들어서 그 평균·표준편차를 구할 수도 있습니다.
# vertex_property_example.py
import graph_tool.all as gt
import numpy as np
def main():
# 1) 그래프 생성
g = gt.Graph()
g.add_vertex(5) # 5개 노드
# 간단히 연결 (예: 0->1, 1->2,...)
g.add_edge(0,1)
g.add_edge(1,2)
g.add_edge(2,3)
g.add_edge(3,4)
# 2) 정점 프로퍼티(예: node_value) 생성
node_val = g.new_vertex_property("float")
# 3) 임의 값 할당
for v in g.vertices():
node_val[v] = np.random.uniform(10, 50) # 10~50 범위 임의
# 4) vertex_average
avg, std = gt.vertex_average(g, deg=node_val) # 'deg' 인자에 property
print("Node property average:", avg)
print("Node property std dev:", std)
if __name__=="__main__":
main()
node_val이라는 VertexPropertyMap("float")에 임의의 실수를 할당.vertex_average(g, deg=node_val)로 평균, 표준편차 계산.Node property average: 29.8123
Node property std dev: 10.564vertex_average()가 반환하는 std는 모집단 표준편차가 아닌, 샘플 표준편차(sample standard deviation, Bessel’s correction) 여부는 문서에서 명확히 언급되지 않았지만, 보통 sqrt( (Σ(x-avg)^2)/N ) 형태로 추정됩니다.O(V): 정점을 한 번씩 스캔해 deg(또는 property) 합계와 제곱합계, 그리고 N(=|V|)로부터 평균·분산·표준편차를 계산.평균이나 표준편차 자체는 숫자 2개이지만,
vertex_hist() (함께 사용)matplotlib에서 plt.boxplot([node_val[v] for v in g.vertices()]) 등.vertex_average()는 그래프의 정점 기반 속성(차수나 임의 프로퍼티)의 평균과 표준편차를 단 한 번에 쉽게 구해줌.avg, std = gt.vertex_average(g, "out")(avg, std)이상으로 vertex_average()의 사용법, 예시 코드, 응용 아이디어를 살펴보았습니다. 도시·교통·네트워크 분석에서 정점별 속성(차수·가중·특성 값)에 대한 전역 통계량(평균, 표준편차)을 빠르고 간단히 구해볼 때 매우 유용합니다.
아래 튜토리얼은 graph-tool 라이브러리의 edge_average() 함수를 이용해서, 그래프의 간선(edge)에 대해 특정 속성(Property) 값의 전체 평균과 표준편차를 간단히 계산하는 방법을 설명합니다. 초보자를 위해 파이썬 코드 예시, 상세한 한글 주석, 응용 팁을 포함했습니다.
edge_average()란?avg, std = graph_tool.stats.edge_average(g, eprop)
g: Graph 객체eprop: EdgePropertyMap, 간선별로 저장된 실수(double) 형태 속성 (예: 길이, 가중치, 비용 등).avg: 주어진 간선 속성값들의 전체 평균(부동소수점)std: 그 평균에 대한 표준편차eprop[e](속성값)를 더하여 평균을 구하고, 분산(표준편차)도 함께 계산.다음 코드는 무작위 그래프를 만들고, 각 간선에 난수(0..1)를 할당한 뒤 edge_average()로 평균과 표준편차를 계산하는 예입니다.
# edge_average_tutorial.py
import graph_tool.all as gt
import numpy as np
def main():
# 1) 그래프 생성: 1000개 노드, 각 노드는 차수=5 근사 (무방향)
g = gt.random_graph(1000, lambda: (5, 5), directed=False)
print("Number of vertices:", g.num_vertices()) # 1000
print("Number of edges:", g.num_edges()) # 대략 2500 정도
# 2) edge property 생성 (double type)
eprop = g.new_edge_property("double")
# 3) 임의 난수 할당: 0..1 범위
arr = eprop.get_array()
arr[:] = np.random.random(g.num_edges())
# 4) edge_average: (평균, 표준편차) 반환
avg, std = gt.edge_average(g, eprop)
print("Edge property average:", avg)
print("Edge property std:", std)
if __name__=="__main__":
main()
random_graph(1000, lambda: (5,5))로 1000개 노드, 각 노드 차수는 평균 5(무방향이면 이웃 합=10?). 실제 간선 수는 무작위로 약 2500 근방.EdgePropertyMap 만들기: eprop = g.new_edge_property("double").arr[:] = np.random.random(g.num_edges()) → [0..1) 범위.edge_average(g, eprop): 모든 간선에 대한 속성값의 평균과 표준편차 계산.Edge property average: 0.4982
Edge property std: 0.00298아래는 도시 도로망에서 간선에 길이를 저장했다고 가정:
# edge_length_example.py
import graph_tool.all as gt
def main():
# 예: g는 이미 로드된 도로망 그래프
# 각 간선에 "length"라는 double property가 있다고 가정
length_prop = g.edge_properties["length"]
avg_len, std_len = gt.edge_average(g, length_prop)
print("Average road length:", avg_len)
print("Std dev:", std_len)
이처럼 실제 분석 시, 도로 길이, 비용, 혼잡도 등등 “간선”에 대응되는 어떤 값이든 평균·표준편차를 한 번에 구할 수 있습니다.
std) 계산 의미std는 (모집단) 표준편차인지, (샘플) 표준편차인지는 문서에 명시X이나, 보통edge_average()는 숫자 2개(평균, 표준편차)만 반환. 히스토그램은 edge_hist() 참고."평균 ± 2*표준편차" 범위로 이상치(Outlier)를 찾는 등 다양한 응용 가능.edge_average(): 그래프의 간선별 속성(실수)에 대해 전체 평균, 표준편차를 손쉽게 얻는 함수.g: Graph 객체eprop: EdgePropertyMap("double")(average: float, std: float)O(E), 병렬화 지원 (OpenMP).이상으로 edge_average() 함수에 대한 친절한 튜토리얼을 마쳤습니다. 도시·교통·네트워크 빅데이터에서 간선 중심 속성(도로 길이, 혼잡도, 비용 등)에 대한 요약 통계를 구하고 싶을 때 간단히 활용할 수 있습니다.
아래 튜토리얼은 graph-tool 라이브러리에서 제공하는 distance_histogram() 함수를 이용해, 그래프 상에서 두 정점 사이의 최단거리(shortest path distance) 분포를 효율적으로 계산하고 시각화하는 방법을 설명합니다. 초보자를 위해 파이썬 예시 코드, 친절한 한글 주석, 사용 팁을 포함했습니다.
distance_histogram() 개요counts, bins = graph_tool.stats.distance_histogram(
g,
weight=None,
bins=[0, 1],
samples=None,
float_count=True
)
g: Graph 객체weight: (선택) EdgePropertyMap (간선 가중치) – 없으면 비가중 최단거리bins: 히스토그램 구간. [0,1] 형식이면 0부터 최대 거리까지 폭=1씩 자동 생성samples: (선택) 샘플링 개수. (None이면 전체 정점 쌍, 정수면 특정 수의 소스 정점을 무작위 선택해 최단거리)float_count: True면 bin 카운트를 float, False면 intcounts: 각 bin별 최단거리 개수(기본 float)bins: bin 경계값(길이=bin 개수+1)samples=None일 때, Floyd-Warshall가 아니라 각 정점을 소스 삼아 BFS (이론상 (O(V \times (V+E))))를 병렬화. 실제 graph-tool 구현 세부 방식에 따라 최적화되어 있음.O(V \times E \log V), 샘플링(s>0) 시는 O(s \times (V+E)) or O(s \times E \log V).다음은 무방향 그래프(무가중)에 대해 모든 정점 쌍의 최단거리 분포(=distance histogram)를 계산하는 예시 코드입니다.
# distance_histogram_tutorial.py
import graph_tool.all as gt
import numpy as np
import matplotlib.pyplot as plt
def main():
# 1) 예시 그래프 생성 (랜덤 그래프)
# - 100개의 노드, 각 노드 차수 ~3 근사
g = gt.random_graph(100, lambda: (3,3), directed=False)
print("Num vertices:", g.num_vertices())
print("Num edges:", g.num_edges())
# 2) distance_histogram: 무가중 => weight=None
# bins=[0,1] => 0부터 최대거리까지 폭=1씩 bin 생성
counts, bin_edges = gt.distance_histogram(
g,
weight=None,
bins=[0,1],
samples=None, # 모든 소스 (전체 정점) 사용
float_count=True
)
# 3) 결과 확인
print("Counts:", counts)
print("Bin edges:", bin_edges)
# 4) 시각화 (막대 그래프)
bin_centers = 0.5*(bin_edges[:-1] + bin_edges[1:])
plt.figure()
plt.bar(bin_centers, counts, width=1.0, align='center', alpha=0.7)
plt.title("Distance histogram (unweighted) of the graph")
plt.xlabel("Distance")
plt.ylabel("Count")
plt.grid(True)
plt.show()
if __name__=="__main__":
main()
gt.random_graph(100, lambda: (3,3), directed=False)로 100개 노드, 평균 차수 ~3인 무작위 그래프.distance_histogram(g, weight=None, bins=[0,1]): counts: 각 거리값이 bin에 얼마나 속했는지(실제로는 0인 bin은 “자기 자신”=0, 일반적 의미에서 count=0 or 별도 처리).samples)을 활용한 최단거리 분포 추정전체 정점 쌍이 아주 많으면(수십만 노드 이상) 계산이 부담될 수 있으므로, samples로 소스 정점 수 제한 → 무작위 s개 소스에서만 BFS (또는 Dijkstra) 실행 후, 나머지 노드까지 거리 측정.
counts_s, bins_s = gt.distance_histogram(
g,
weight=None,
bins=[0,1],
samples=10, # 10개 정점만 소스로 사용
float_count=True
)
counts_s, bins_s는 실제 전역 분포의 근사치.EdgePropertyMap("double") 형태의 가중치(예: 거리, 비용, 시간 등) → Dijkstra류 알고리즘으로 최단거리.distance_histogram(g, weight=weight_prop, ...) 식으로 호출.counts[0]이 0 (혹은 특정 값)일 수 있음. 보통 “자기 자신에게 거리=0”이 V번 있겠으나, graph-tool에서 이 부분(자기 자신)은 보정할 수도 있으니 결과 해석 시 주의.∞로 취급 → histogram에 포함 안 되거나, maxDistance보다 큰 값. 예시 (cumulative 형태):
cum_counts = np.cumsum(counts / counts.sum())
plt.plot(bin_centers, cum_counts, marker='o')
distance_histogram(): 그래프에서 모든(혹은 샘플링된) 정점 쌍의 최단거리 분포를 bin 단위로 세어 줌.counts, bins = gt.distance_histogram(g, weight=..., bins=[0,1], samples=None)samples로 근사.이상으로 distance_histogram()의 기능, 예시 코드, 활용을 살펴보았습니다. 도시·교통 네트워크에서 최단 경로분포(ex. 평균 이동 거리, 페어와이즈 접근성, 커뮤니티 분리 정도 등)를 평가할 때 매우 유용합니다.