bipartite 키워드
영상에서 Eric이 'bipartite' 키워드를 소개했어요. 이 키워드는 노드의 메타데이터 딕셔너리의 일부이며, 노드를 추가할 때도, 추가한 이후에도 지정할 수 있어요. 다만, 이분 그래프의 정의에 따르면 같은 파티션에 속한 노드끼리는 서로 연결될 수 없다는 점을 기억하세요.
여기서는 이분 그래프에서 지정한 파티션에 속한 노드만 반환하는 함수를 작성해 볼 거예요. 이번에 다룰 Github 이분 그래프의 관련 파티션은 'projects'와 'users'예요.
이 연습은 강의의 일부입니다
Python 중급 네트워크 분석
연습 안내
get_nodes_from_partition()라는 함수를 작성하세요. 이 함수는 두 개의 인수, 즉 이분 그래프G와 그G의partition을 받아 해당partition에 속한 노드만 반환해야 해요.for루프를 사용해G의 모든 노드(메타데이터는 제외)를 순회하세요.- 현재 노드의 메타데이터 딕셔너리에서
'bipartite'키워드에 접근하세요. 그 값이partition과 같다면 현재 노드를 리스트nodes에 추가하세요.
get_nodes_from_partition()함수와len()함수를 함께 사용해 다음을 수행하세요.G의'projects'파티션에 있는 노드 수를 출력하세요.G의'users'파티션에 있는 노드 수를 출력하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Define get_nodes_from_partition()
def ____:
# Initialize an empty list for nodes to be returned
nodes = []
# Iterate over each node in the graph G
for n in ____:
# Check that the node belongs to the particular partition
if G.nodes[n]['____'] == ____:
# If so, append it to the list of nodes
____
return nodes
# Print the number of nodes in the 'projects' partition
print(____(get_nodes_from_partition(____, '____')))
# Print the number of nodes in the 'users' partition
print(____(____))