일지

자료구조...79

niamdank 2021. 4. 21. 20:45

ArrayGraph 기능 메서드)

그래프에서 사용하는 기능 메서드를 구현한다.

 

ArrayGraph.cpp

/// <summary>
/// 지정된 인덱스의 노드에 진입하는 차수를 반환한다.
/// </summary>
/// <param name="index">노드의 인덱스</param>
size_t ArrayGraph::GetDegreeIn(int index)
{
	if (index < 0 || index >= m_nodeCount)
	{
		return -1;
	}

	int count{ 0 };
	for (int i = 0; i < m_nodeCount; i++)
	{
		count += m_graph[i][index];
	}

	return count;
}

/// <summary>
/// 지정된 인덱스의 노드에서 진출하는 차수를 반환한다.
/// </summary>
/// <param name="index">노드의 인덱스</param>
size_t ArrayGraph::GetDegreeOut(int index)
{
	if (index < 0 || index >= m_nodeCount)
	{
		return -1;
	}

	int count{ 0 };
	for (int i = 0; i < m_nodeCount; i++)
	{
		count += m_graph[index][i];
	}

	return count;
}

 

main.cpp

#include <iostream>
#include "Graph/ArrayGraph.h"

// 생성한 자료구조 테스트용 메인
int main()
{
	ArrayGraph graph;

	graph.InsertNode();
	graph.InsertNode();
	graph.InsertNode();
	graph.InsertNode();

	graph.InsertEdge(0, 1);
	graph.InsertEdge(0, 3);
	graph.InsertEdge(1, 1);
	graph.InsertEdge(1, 2);
	graph.InsertEdge(1, 3);
	graph.InsertEdge(2, 0);
	graph.InsertEdge(2, 3);
	graph.InsertEdge(3, 0);
	graph.PrintInfo();

	std::cout << graph.GetDegreeIn(2) << ' ' << graph.GetDegreeOut(2) << '\n';

	graph.RemoveEdge(3, 0);
	graph.PrintInfo();

	std::cout << graph.GetDegreeIn(2) << ' ' << graph.GetDegreeOut(2) << '\n';

	graph.RemoveNode(1);
	graph.PrintInfo();

	std::cout << graph.GetDegreeIn(2) << ' ' << graph.GetDegreeOut(2) << '\n';

	graph.Clear();
	graph.PrintInfo();
}

 

실행결과

----------------------
Capacity: 5
Count: 4
 0  1  0  1  0
 0  1  1  1  0
 1  0  0  1  0
 1  0  0  0  0
 0  0  0  0  0
----------------------

1 2
----------------------
Capacity: 5
Count: 4
 0  1  0  1  0
 0  1  1  1  0
 1  0  0  1  0
 0  0  0  0  0
 0  0  0  0  0
----------------------

1 2
----------------------
Capacity: 5
Count: 3
 0  0  1  0  0
 1  0  1  0  0
 0  0  0  0  0
 0  0  0  0  0
 0  0  0  0  0
----------------------

2 0
----------------------
Capacity: 5
Count: 0
 0  0  0  0  0
 0  0  0  0  0
 0  0  0  0  0
 0  0  0  0  0
 0  0  0  0  0
----------------------