Skip to content

Commit

Permalink
[Graph] Add Adj-Matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
deunlee committed May 29, 2020
1 parent 914e955 commit 30ded86
Show file tree
Hide file tree
Showing 9 changed files with 369 additions and 10 deletions.
10 changes: 10 additions & 0 deletions Data-Structure.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Deque", "Deque\Deque.vcxpro
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LinkedList", "LinkedList\LinkedList.vcxproj", "{57412A8E-41D0-474D-B912-9AAB3EBDA451}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Graph", "Graph\Graph.vcxproj", "{6051C310-D82F-405A-A84D-64C9FD818FFE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Expand Down Expand Up @@ -61,6 +63,14 @@ Global
{57412A8E-41D0-474D-B912-9AAB3EBDA451}.Release|x64.Build.0 = Release|x64
{57412A8E-41D0-474D-B912-9AAB3EBDA451}.Release|x86.ActiveCfg = Release|Win32
{57412A8E-41D0-474D-B912-9AAB3EBDA451}.Release|x86.Build.0 = Release|Win32
{6051C310-D82F-405A-A84D-64C9FD818FFE}.Debug|x64.ActiveCfg = Debug|x64
{6051C310-D82F-405A-A84D-64C9FD818FFE}.Debug|x64.Build.0 = Debug|x64
{6051C310-D82F-405A-A84D-64C9FD818FFE}.Debug|x86.ActiveCfg = Debug|Win32
{6051C310-D82F-405A-A84D-64C9FD818FFE}.Debug|x86.Build.0 = Debug|Win32
{6051C310-D82F-405A-A84D-64C9FD818FFE}.Release|x64.ActiveCfg = Release|x64
{6051C310-D82F-405A-A84D-64C9FD818FFE}.Release|x64.Build.0 = Release|x64
{6051C310-D82F-405A-A84D-64C9FD818FFE}.Release|x86.ActiveCfg = Release|Win32
{6051C310-D82F-405A-A84D-64C9FD818FFE}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
95 changes: 95 additions & 0 deletions Graph/AdjacencyMatrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#ifndef __DEUN_GRAPH_ADJ_MATRIX_CPP__
#define __DEUN_GRAPH_ADJ_MATRIX_CPP__

#include "AdjacencyMatrix.h"

namespace Deun {
AdjacencyMatrix::AdjacencyMatrix(int vSize) {
if (vSize <= 0) {
this->vSize = 0;
throw AdjacencyMatrixError::MEMORY_ALLOCATION_FAILED;
}

this->vSize = vSize;
vCount = 0;
matrix = new (std::nothrow) char[vSize * vSize];

if (!matrix) {
this->vSize = 0;
throw AdjacencyMatrixError::MEMORY_ALLOCATION_FAILED;
}

clear();
}

AdjacencyMatrix::~AdjacencyMatrix() {
delete[] matrix;
}

inline char AdjacencyMatrix::getRaw(int from, int to) {
return matrix[from * vSize + to];
}

inline void AdjacencyMatrix::setRaw(int from, int to, char value) {
matrix[from * vSize + to] = value;
}

int AdjacencyMatrix::insertVertex() {
if (vCount < vSize) {
return vCount++;
}
throw AdjacencyMatrixError::TOO_MANY_VERTICES;
}

bool AdjacencyMatrix::insertEdge(int from, int to, bool undirected) {
if (from < 0 || from >= vCount || to < 0 || to >= vCount) {
return false;
}

setRaw(from, to, 1);
if (undirected) {
setRaw(to, from, 1);
}
return true;
}

bool AdjacencyMatrix::hasVertex(int v) {
if (v >= 0 && v < vCount) {
return true;
}
return false;
}

bool AdjacencyMatrix::hasEdge(int from, int to) {
if (from < 0 || from >= vCount || to < 0 || to >= vCount) {
return false;
}
return getRaw(from, to);
}

void AdjacencyMatrix::clear() {
for (unsigned int i = 0; i < (unsigned int)vSize * (unsigned int)vSize; i++) {
matrix[i] = 0;
}
}

void AdjacencyMatrix::print() {
using namespace std;

cout << "AdjacencyMatrix(vSize=" << vSize << ", vCount=" << vCount << ")" << endl;

if (vCount) {
for (int i = 0; i < vCount; i++) {
for (int j = 0; j < vCount; j++) {
cout << (int)getRaw(i, j) << " ";
}
cout << endl;
}
}
else {
cout << "(empty)" << endl;
}
}
}

#endif
25 changes: 15 additions & 10 deletions Graph/AdjacencyMatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ namespace Deun {
TOO_MANY_VERTICES,
};

// 배열 기반 인접 행렬 그래프
/**
* 배열 기반 인접 행렬 그래프
*/
class AdjacencyMatrix {
private:
protected:
int vSize; // 정점의 최대 개수 (메모리 할당량)
int vCount; // 정점의 개수
char* matrix; // 인접 행렬

inline char getRaw(int from, int to);
inline void setRaw(int from, int to, char value);

public:
/**
* 인접 행렬 생성자
Expand All @@ -41,29 +46,29 @@ namespace Deun {
* 간선은 from과 to를 연결하며 방향성이 있습니다.
* undirected가 true인 경우에는 to와 from을 잇는 간선도 삽입합니다.
*
* @param {int} from: 시작 정점
* @param {int} to: 끝 정점
* @param {bool} undirected: 무방향 그래프 여부 (default: false)
* @param {int} from: 시작 정점 인덱스(0-based)
* @param {int} to: 끝 정점 인덱스(0-based)
* @param {bool} undirected: 무방향 그래프 여부
* @return {bool} 성공 여부
*/
bool insertEdge(int from, int to, bool undirected = false);

/**
* 정점의 존재 여부를 반환합니다.
*
* @param {int} v: 정점 번호
* @param {int} v: 정점 인덱스(0-based)
* @return {bool} 존재 여부
*/
inline bool hasVertex(int v);
bool hasVertex(int v);

/**
* 간선의 존재 여부를 반환합니다.
*
* @param {int} from: 시작 정점
* @param {int} to: 끝 정점
* @param {int} from: 시작 정점 인덱스(0-based)
* @param {int} to: 끝 정점 인덱스(0-based)
* @return {bool} 존재 여부
*/
inline bool hasEdge(int from, int to);
bool hasEdge(int from, int to);

/**
* 인접 행렬을 초기화합니다.
Expand Down
6 changes: 6 additions & 0 deletions Graph/Graph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef __DEUN_GRAPH_CPP__
#define __DEUN_GRAPH_CPP__



#endif
6 changes: 6 additions & 0 deletions Graph/Graph.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef __DEUN_GRAPH_H__
#define __DEUN_GRAPH_H__



#endif
163 changes: 163 additions & 0 deletions Graph/Graph.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http:https://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>15.0</VCProjectVersion>
<ProjectGuid>{6051C310-D82F-405A-A84D-64C9FD818FFE}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>Graph</RootNamespace>
<WindowsTargetPlatformVersion>10.0.10240.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="AdjacencyList.h" />
<ClInclude Include="AdjacencyMatrix.h" />
<ClInclude Include="Graph.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="AdjacencyList.cpp" />
<ClCompile Include="AdjacencyMatrix.cpp" />
<ClCompile Include="Graph.cpp" />
<ClCompile Include="Main.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
14 changes: 14 additions & 0 deletions Graph/Graph.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http:https://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ClInclude Include="AdjacencyList.h" />
<ClInclude Include="AdjacencyMatrix.h" />
<ClInclude Include="Graph.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="Graph.cpp" />
<ClCompile Include="AdjacencyMatrix.cpp" />
<ClCompile Include="AdjacencyList.cpp" />
<ClCompile Include="Main.cpp" />
</ItemGroup>
</Project>
4 changes: 4 additions & 0 deletions Graph/Graph.vcxproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http:https://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>
Loading

0 comments on commit 30ded86

Please sign in to comment.