algorithm bicoloring - andstudy/forge GitHub Wiki

CynicJJ

  • Vertex ๋Š” NONE, BLUE, RED 3๊ฐ€์ง€ ์ƒํƒœ๋ฅผ ๊ฐ€์ง

  • Edge ๋Š” 2๊ฐœ์˜ Vertex index ๋กœ ์ด๋ฃจ์–ด์ง

    • ๋”ฐ๋กœ ํด๋ž˜์Šค๋กœ ๋งŒ๋“ค์ง€๋Š” ์•Š์•˜์Œ
  • !ProcessEdge() ์—์„œ 2๊ฐœ์˜ Vertex ์ƒ‰์„ ๋‹ค๋ฅด๊ฒŒ ๋งŒ๋“ ๋‹ค

  • ์ˆจ๊ฒจ์ง„ ์ œ์•ฝ์‚ฌํ•ญ - Edge ์ฒ˜๋ฆฌ ์ˆœ์„œ๋ฅผ ์ง€์ผœ์•ผ ํ•œ๋‹ค

    • ๋‚ฎ์€ ๋ฒˆํ˜ธ์˜ Vertex ๋ถ€ํ„ฐ ์ฒ˜๋ฆฌ ํ•ด์•ผํ•จ
  • ๋ฆฌํŒฉํ† ๋ง์˜ ์—ฌ์ง€๊ฐ€ ๋” ์žˆ์œผ๋‚˜ ๊ท€์ฐฎ์•„์„œ...

Bicoloring.vb

    Public Class Bicoloring
    
    	Private _vertexes As List(Of Vertex) = New List(Of Vertex)
    	Private _edges As List(Of List(Of Integer)) = New List(Of List(Of Integer))
    
    	Function GetVertex(ByVal index As Integer) As Vertex
    		Return _vertexes(index)
    	End Function
    
    	Sub SetupEdges(ByVal inputs As List(Of String))
    		_edges.Clear()
    
    		Dim separator() As Char = {" "c}
    
    		Dim edgeCount = Integer.Parse(inputs(1))
    		For i As Integer = 1 To edgeCount
    			Dim input() As String = inputs(i + 1).Split(separator, StringSplitOptions.RemoveEmptyEntries)
    
    			Dim first As Integer = Integer.Parse(input(0))
    			Dim second As Integer = Integer.Parse(input(1))
    
    			Dim edge As List(Of Integer) = New List(Of Integer)
    			edge.Add(Math.Min(first, second))
    			edge.Add(Math.Max(first, second))
    
    			_edges.Add(edge)
    		Next
    	End Sub
    
    	Function IsBicolorable(ByVal inputs As List(Of String)) As Boolean
    		Dim vertexCount As Integer = Integer.Parse(inputs(0))
    		SetupVertexes(vertexCount)
    
    		SetupEdges(inputs)
    
    		ProcessAllEdges(_edges)
    
    		Dim result As Boolean = IsBicolor(_edges)
    		Return result
    	End Function
    
    	Function IsBicolor(ByVal edges As List(Of List(Of Integer))) As Boolean
    		For Each edge As List(Of Integer) In edges
    			Dim first As Vertex = GetVertex(edge(0))
    			Dim second As Vertex = GetVertex(edge(1))
    
    			If first.Color = second.Color Then
    				Return False
    			ElseIf first.Color = COLOR.NONE Or second.Color = COLOR.NONE Then
    				Return False
    			End If
    		Next
    		Return True
    	End Function
    
    	Sub ProcessAllEdges(ByVal edges As List(Of List(Of Integer)))
    		For i As Integer = 0 To _edges.Count - 1
    			For Each edge As List(Of Integer) In edges
    				If edge(0) = i Then
    					ProcessEdge(GetVertex(edge(0)), GetVertex(edge(1)))
    				End If
    			Next
    		Next
    	End Sub
    
    	Sub SetupVertexes(ByVal count As Integer)
    		_vertexes.Clear()
    		For i As Integer = 1 To count
    			_vertexes.Add(New Vertex)
    		Next
    	End Sub
    
    	Function GetVertexCount() As Integer
    		Return _vertexes.Count
    	End Function
    
    	Sub ProcessEdge(ByVal first As Vertex, ByVal second As Vertex)
    		If first.Color = COLOR.RED And second.Color = COLOR.NONE Then
    			second.SetColor(COLOR.BLUE)
    		ElseIf first.Color = COLOR.BLUE And second.Color = COLOR.NONE Then
    			second.SetColor(COLOR.RED)
    		ElseIf first.Color = COLOR.NONE And second.Color = COLOR.RED Then
    			first.SetColor(COLOR.BLUE)
    		ElseIf first.Color = COLOR.NONE And second.Color = COLOR.BLUE Then
    			first.SetColor(COLOR.RED)
    		ElseIf first.Color = COLOR.NONE And second.Color = COLOR.NONE Then
    			first.SetColor(COLOR.BLUE)
    			second.SetColor(COLOR.RED)
    		End If
    	End Sub
    End Class

COLOR.vb

    Public Enum COLOR
    	NONE
    	BLUE
    	RED
    End Enum

Vertex.vb

    Public Class Vertex
    
    	Private _color As COLOR = Basic.COLOR.NONE
    	ReadOnly Property Color()
    		Get
    			Return _color
    		End Get
    	End Property
    
    	Sub SetColor(ByVal aColor As COLOR)
    		_color = aColor
    	End Sub
    End Class

BiColoringTest.vb

    <TestClass()> _
    Public Class BicoloringTest
    
    	Private _bicolor As Bicoloring = New Bicoloring
    	Private _inputs1 As List(Of String) = New List(Of String)
    	Private _inputs2 As List(Of String) = New List(Of String)
    	Private _inputs3 As List(Of String) = New List(Of String)
    
    	Public Sub New()
    		_inputs1.Add("3")
    		_inputs1.Add("3")
    		_inputs1.Add("0 1")
    		_inputs1.Add("1 2")
    		_inputs1.Add("2 0")
    
    		_inputs2.Add("9")
    		_inputs2.Add("8")
    		_inputs2.Add("0 1")
    		_inputs2.Add("0 2")
    		_inputs2.Add("0 3")
    		_inputs2.Add("0 4")
    		_inputs2.Add("0 5")
    		_inputs2.Add("0 6")
    		_inputs2.Add("0 7")
    		_inputs2.Add("0 8")
    
    		_inputs3.Add("4")
    		_inputs3.Add("3")
    		_inputs3.Add("0 2")
    		_inputs3.Add("2 1")
    		_inputs3.Add("3 0")
    	End Sub
    
    	<TestMethod()> _
    	Public Sub SetVertexCountTest()
    		_bicolor.SetupVertexes(3)
    		Assert.AreEqual(3, _bicolor.GetVertexCount())
    		_bicolor.SetupVertexes(9)
    		Assert.AreEqual(9, _bicolor.GetVertexCount())
    	End Sub
    
    	<TestMethod()> _
    	Public Sub ProcessEdgeTest()
    		_bicolor.SetupVertexes(2)
    		Dim first As Vertex = _bicolor.GetVertex(0)
    		Dim second As Vertex = _bicolor.GetVertex(1)
    		second.SetColor(COLOR.BLUE)
    
    		_bicolor.ProcessEdge(first, second)
    		Assert.AreEqual(COLOR.RED, first.Color)
    	End Sub
    
    	<TestMethod()> _
    	Public Sub IsBicolorableTest()
    		Assert.IsFalse(_bicolor.IsBicolorable(_inputs1))
    		Assert.IsTrue(_bicolor.IsBicolorable(_inputs2))
    		Assert.IsTrue(_bicolor.IsBicolorable(_inputs3))
    	End Sub
    
    End Class

Outbreak

๋ฌธ์ œ ์š”์•ฝ

  • 2๊ฐ€์ง€ ์ƒ‰์œผ๋กœ ๋…ธ๋“œ๋ฅผ ์น  ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์ธ์ ‘ํ•œ ๋…ธ๋“œ๊ฐ„์—๋Š” ๋ฐ˜๋“œ์‹œ ๋‹ค๋ฅธ ์ƒ‰๊น”์„ ๊ฐ€์ ธ์•ผ ํ•œ๋‹ค.
  • 2๊ฐ€์ง€ ์ƒ‰์œผ๋กœ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ์น  ํ•  ์ˆ˜ ์žˆ๋Š”๊ฐ€?

๋ฌธ์ œ ํ•ด๊ฒฐ

  • ๊ฒ€์ •, ํฐ์ƒ‰ ๋‘๊ฐ€์ง€ ์ƒ‰์ด๋ผ๊ณ  ํ–ˆ์„ ๋•Œ
    • ์ฒซ ๋…ธ๋“œ๋ฅผ ๊ฒ€์ •์œผ๋กœ ์น ํ•˜๊ณ  ์ธ์ ‘๋…ธ๋“œ๋ฅผ ํ•˜๋‚˜์”ฉ ์น ํ•ด๊ฐ.
    • ์ฒซ ๋…ธ๋“œ๋ฅผ ํฐ์ƒ‰์œผ๋กœ ์น ํ•˜๊ณ  ์ธ์ ‘๋…ธ๋“œ๋ฅผ ํ•˜๋‚˜์”ฉ ์น ํ•ด๊ฐ.
  • ์œ„ ๋‘ ๊ฐ€์ง€๋ฅผ ์‹œ๋„ํ•˜๊ณ  ํ•˜๋‚˜๋ผ๋„ ๋˜๋ฉด Bicoloring ๊ฐ€๋Šฅ

ํ’€์ด

    ์ „์ฒ ์—์„œ ใ„ฑใ„ฑ

ParkPD

  • ์ƒ๊ฐ๋ณด๋‹ค ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๋Š”๊ตฐ์š”. :(

      /* @JUDGE_ID:parkpd 110401 Cpp "test" */
      
      /* @BEGIN_OF_SOURCE_CODE */
      
      #include <iostream>
      #include <vector>
      #include <set>
      #include <string>
      #include <strstream>
      #include <algorithm>
      #include <map>
      
      //#define _UNIT_TEST
      
      using namespace std;
      
      namespace ATUtil
      {
      
      bool IsInRange(int value, int from, int to)
      {
      	return (from <= value) && (value <= to);
      }
      
      int ConvertToIndex(char c)
      {
      	if ('a' <= c && c <= 'z')
      	{
      		return c - 'a';
      	}
      	else
      	{
      		return -1;
      	}
      }
      
      char ConvertToChar(int i)
      {
      	return (char)i + 'a';
      }
      
      typedef map<int, int> IntDB;
      typedef vector<int> Ints;
      
      };
      
      using namespace ATUtil;
      
      #ifdef _UNIT_TEST
      
      #include "../UnitTest++/src/UnitTest++.h"
      
      int main()
      {
      	UnitTest::RunAllTests();
      
      	char temp;
      	cin >> temp;
      
      	return 0;
      }
      
      #endif
      
      // code implement
      
      namespace ATCode
      {
      
      ///////////////////////////////////////////////////////////////
      // CBicoloring
      class CBicoloring
      {
      public:
      	enum
      	{
      		MAX_SIZE = 200
      	};
      
      	enum Color
      	{
      		EMPTY = 0,
      		RED = 1,
      		BLUE = 2,
      		INVALID
      	};
      
      	CBicoloring();
      	void SetVerticesNum(int num);
      	void Connect(int from, int to);
      	bool IsBicolorable();
      	bool _IsBicolorable(int from);
      	int GetAdjectiveVertex(int from, Ints& ints);
      	string Result();
      
      	struct Vertex
      	{
      		Vertex(int index) : m_Index(index), m_Color(EMPTY) {}
      		Color GetOppositeColor()
      		{
      			if (m_Color == RED)
      			{
      				return BLUE;
      			}
      			else if (m_Color == BLUE)
      			{
      				return RED;
      			}
      			else
      			{
      				//_ASSERT(0);
      				return INVALID;
      			}
      		}
      
      		int m_Index;
      		Color m_Color;
      	};
      
      	std::vector<Vertex> m_Vertex;
      	int m_nVertexNum;
      	bool m_GraphEdges[MAX_SIZE][MAX_SIZE];
      };
      
      CBicoloring::CBicoloring() : m_nVertexNum(0)
      {
      	for (int i = 0; i < MAX_SIZE; ++i)
      	{
      		for (int j = 0; j < MAX_SIZE; ++j)
      		{
      			m_GraphEdges[i][j] = false;
      		}
      	}
      }
      
      void CBicoloring::SetVerticesNum(int num)
      {
      	m_Vertex.reserve(num);
      	for (int i = 0; i < num; ++i)
      	{
      		m_Vertex.push_back(Vertex(i));
      	}
      
      	m_nVertexNum = num;
      }
      
      void CBicoloring::Connect(int from, int to)
      {
      	m_GraphEdges[from][to] = true;
      	m_GraphEdges[to][from] = true;
      }
      
      bool CBicoloring::IsBicolorable()
      {
      	// ์ „์ฒด vertex ๋ฅผ ์ญ‰ ๋Œ๋ฉด์„œ starting ์ง€์ ์œผ๋กœ ์‚ผ๊ณ ,
      	// ์ธ์ ‘ vertex ์ƒ‰์ด 
      		// ์•ˆ ์น ํ•ด์ ธ ์žˆ๋‹ค๋ฉด, ๋‚˜์™€ ๋‹ค๋ฅธ ์ƒ‰์œผ๋กœ ์น ํ•˜๊ณ ,
      		// ๋‹ค๋ฅด๋ฉด ํ†ต๊ณผ
      		// ๊ฐ™์œผ๋ฉด ์‹คํŒจ
      
      	Vertex& s = m_Vertex[0];
      	s.m_Color = BLUE;
      
      	return _IsBicolorable(0);
      }
      
      bool CBicoloring::_IsBicolorable(int from)
      {
      	Vertex& s = m_Vertex[from];
      
      	Ints adjectives;
      	GetAdjectiveVertex(from, adjectives);
      
      	bool ret = false;
      
      	for (size_t i = 0; i < adjectives.size(); ++i)
      	{
      		int to = adjectives[i];
      		Vertex& v = m_Vertex[to];
      
      		// ์ธ์ ‘ vertex ์ƒ‰์ด 
      		if (EMPTY == v.m_Color)	// ์•ˆ ์น ํ•ด์ ธ ์žˆ๋‹ค๋ฉด, ๋‚˜์™€ ๋‹ค๋ฅธ ์ƒ‰์œผ๋กœ ์น ํ•˜๊ณ ,
      		{
      			v.m_Color = s.GetOppositeColor();
      			ret = _IsBicolorable(to);
      		}
      		else if (s.m_Color != v.m_Color)
      		{
      			// OK
      			ret = _IsBicolorable(to);
      		}
      		else	// ์ธ์ ‘ vertex ๊ฐ€ ๋‚˜์™€ ๊ฐ™์€ ์ƒ‰. ์ทŸ...
      		{
      			ret = false;
      		}
      
      		if (!ret)
      		{
      			return false;
      		}
      	}
      
      	return true;
      }
      
      int CBicoloring::GetAdjectiveVertex(int from, Ints& ints)
      {
      	for (int to = 0; to < m_nVertexNum; ++to)
      	{
      		// edge ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๊ณ , ์•„์ง ํ™•์ธ ์•ˆ ๋œ vertex ๋ผ๋ฉด
      		if (m_GraphEdges[from][to] && (m_Vertex[to].m_Color == EMPTY))
      		{
      			ints.push_back(to);
      		}
      	}
      
      	return (int)ints.size();
      }
      
      string CBicoloring::Result()
      {
      	if (IsBicolorable())
      	{
      		return "BICOLORABLE.\n";
      	}
      	else
      	{
      		return "NOT BICOLORABLE.\n";
      	}
      }
      
      ///////////////////////////////////////////////////////////////
      // CConsole
      class CConsole
      {
      public:
      	static void ConsoleTest(istream& input, ostream& output);
      };
      
      void CConsole::ConsoleTest(istream& input, ostream& output)
      {
      	//ostrstream tempOutput;
      
      	while (1)
      	{
      		int vertices = 0;
      		input >> vertices;
      
      		if (0 == vertices)
      		{
      			break;
      		}
      
      		int edges = 0;
      		input >> edges;
      
      		CBicoloring s;
      		s.SetVerticesNum(vertices);
      
      		int from;
      		int to;
      		for (int i = 0; i < edges; ++i)
      		{
      			input >> from >> to;
      			s.Connect(from, to);
      		}
      
      		output << s.Result();
      
      		//tempOutput << ends;
      		//output << tempOutput.str();
      	}
      };
      
      }
      
      using namespace ATCode;
      
      #ifndef _UNIT_TEST
      
      int main()
      {
      	CConsole::ConsoleTest(cin, cout);
      
      	return 0;
      }
      
      #else
      
      // tests
      
      struct FixtureConsole
      {
      	stringstream input;
      	stringstream output;
      };
      
      TEST_FIXTURE(FixtureConsole, ConsoleTest1)
      {
      	input << "3\n3\n0 1\n1 2\n2 0\n0\n";
      	CConsole::ConsoleTest(input, output);
      	CHECK_EQUAL("NOT BICOLORABLE.\n", output.str());
      }
      
      TEST_FIXTURE(FixtureConsole, ConsoleTest2)
      {
      	input << "9\n8\n0 1\n0 2\n0 3\n0 4\n0 5\n0 6\n0 7\n0 8\n0\n";
      	CConsole::ConsoleTest(input, output);
      	CHECK_EQUAL("BICOLORABLE.\n", output.str());
      }
      
      TEST_FIXTURE(FixtureConsole, ConsoleTest3)
      {
      	input << "3\n2\n0 1\n1 2\n0\n";
      	CConsole::ConsoleTest(input, output);
      	CHECK_EQUAL("BICOLORABLE.\n", output.str());
      }
      
      TEST_FIXTURE(FixtureConsole, ConsoleTest4)
      {
      	input << "3\n2\n0 2\n2 1\n0\n";
      	CConsole::ConsoleTest(input, output);
      	CHECK_EQUAL("BICOLORABLE.\n", output.str());
      }
      
      #endif
      
      /* @END_OF_SOURCE_CODE */
    

Mastojun

๋ฌธ์ œ ํ’€์ด

  • ํ•™๋ถ€ 2ํ•™๋…„๋•Œ ๊ทธ๋ž˜ํ”„ ๋ถ€๋ถ„์„ ์ œ๋Œ€๋กœ ๊ณต๋ถ€๋ฅผ ๋ชปํ•ด์„œ์ธ์ง€ ๊ทธ๋ž˜ํ”„๊ฐ€ ๋‘๋ ค์›Œ์š”.

  • ์ž๋ฃŒ๋Š” Array๋ฅผ ์ด์šฉํ•ด์„œ ํ‘œํ˜„ํ•˜๊ณ , ์ตœ์ดˆ node๋ฅผ ๊ฒ€์ •์ƒ‰์œผ๋กœ ์น ํ•˜๊ณ  ์—ฐ๊ฒฐ๋˜์–ด์žˆ๋Š” node๋“ค์„ ์ฐพ์•„๊ฐ€๋ฉฐ ์ƒ‰์„ ์น ํ•˜๋‹ค๊ฐ€ ์ด์ „ node๋ž‘ ๊ฐ™์€์ƒ‰์˜ ์—ฐ๊ฒฐ๋œ node๋ฅผ ๋ฐœ๊ฒฌํ•˜๋ฉด false๋ฅผ ๋Œ๋ ค์ฃผ๋„๋ก ํ–ˆ์Šต๋‹ˆ๋‹ค.

  • ์ด๋ฒˆ์—” ์†Œ์Šค๊ฐ€ ๊น”๋”ํ•˜์ง€ ๋ชปํ•˜๊ฒŒ ๋‚˜์™”๋„ค์š” ใ… _ใ… ..

์†Œ์Šค ์ฝ”๋“œ

    #include <stdio.h>
    #define ever (;;)
    
    enum COLOR {NONE, BLACK, WHITE};
    
    struct node
    {
    	bool use;
    	int  color;
    	bool isLink[200];
    }Matrix[200];
    
    int n;
    
    void InputData()
    {
    	int l;
    	scanf("%d", &l);
    
    	for(int i = 0; i < n; i++ )
    	{
    		Matrix[i].color = NONE;
    		Matrix[i].use	= false;
    
    		for(int j = 0; j < n; j++ )
    		{
    			Matrix[i].isLink[j] = false;	
    		}
    	}
    
    	for(int i = 0; i < l; i++ )
    	{
    		int temp1, temp2;
    
    		scanf("%d %d", &temp1, &temp2);
    
    		Matrix[temp1].use = true;
    		Matrix[temp2].use = true;
    		Matrix[temp1].isLink[temp2] = true;
    		Matrix[temp2].isLink[temp1] = true;	
    	}
    }
    
    int GetNextColor(int color)
    {
    	color++;
    	if(color > WHITE) color = BLACK;
    
    	return color;
    }
    
    //////////////////////////////////////////////////////////////////////////
    // no์— ์—ฐ๊ฒฐ๋œ node๋“ค์— ์ƒ‰์„ ๋‹ค๋ฅธ์ƒ‰์œผ๋กœ ์น ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜.,
    //  ๋งŒ์•ฝ node์— ์ด๋ฏธ ์ƒ‰์ด ์น ํ•ด์ ธ ์žˆ๋‹ค๋ฉด,. no์˜ ์ƒ‰๊ณผ ๊ฐ™์œผ๋ฉด false๋ฆฌํ„ด
    //////////////////////////////////////////////////////////////////////////
    bool FillColor(int no, int color)
    {
    	int nextColor = GetNextColor(color);
    	
    	for(int i = 0; i < n; i++ )
    	{
    		if( Matrix[no].isLink[i] )
    		{
    			if( Matrix[i].color  == color ) // ์ด๋ฏธ ์ƒ‰์ด ์น ํ•ด์ ธ ์žˆ๋Š”๋ฐ ๊ฐ™๋‹ค๋ฉด..
    			{
    				return false;
    			}
    			else if( Matrix[i].color == NONE ) // ์ƒ‰์ด ์น ํ•ด์ ธ ์žˆ์ง€ ์•Š๋‹ค๋ฉด..
    			{
    				Matrix[i].color = nextColor;	// no์™€ ๋‹ค๋ฅธ์ƒ‰์„ ์น ํ•˜๊ธฐ
    
    				if( FillColor(i, nextColor) == false ) // i์™€ ์—ฐ๊ฒฐ๋œ node๋“ค ํƒ์ƒ‰ ์‹œํ‚ค๊ธฐ!
    				{
    					return false;
    				}
    			}
    		}
    	}
    
    	return true;
    }
    
    
    bool GetResult()
    {
    	for(int i = 0; i < n; i++ )
    	{
    		if( Matrix[i].use )
    		{
    			Matrix[i].color = BLACK;
    
    			return FillColor(i, BLACK);
    		}
    
    	}
    
    	return true;
    }
    
    int main()
    {
    
    	for ever
    	{
    		scanf("%d", &n);
    
    		if( n == 0 ) break;
    
    		InputData();
    		if( GetResult() == false )
    		{
    			printf("NOT ");
    		}
    
    		printf("BICOLORABLE.\n");
    	}
    
    	return 0;
    }

Kukuman

  • ๋ฐฐ์—ด ๋ฐฐ์—ด๋กœ ๊ทธ๋ž˜ํ”„๋ฅผ ์ฒ˜๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค

  • ์†Œ์Šค

      public class Graph {
      	private int pointCount;
      	private boolean[][] nodes;
      	
      	private final int RED=1;
      	private final int BLUE=2;
      	public Graph(int pointCount) {
      		this.pointCount = pointCount;
      		nodes = new boolean[pointCount][pointCount];
      		for(int i=0;i<pointCount;i++){
      			for(int j=0;j<pointCount;j++){
      				nodes[i][j] = false;
      			}
      		}
      	}
      	public void insertNode(int fromPoint, int toPoint) {
      		nodes[fromPoint][toPoint] = true;
      		nodes[toPoint][fromPoint] = true;
      		
      	}
      	public boolean checkBio() {
      		//๋ฐฉ๋ฌธํ–ˆ์œผ๋ฉด ์ƒ‰๊ฐˆ๋ฒˆํ˜ธ๊ฐ€ ์„ค์ •๋˜์–ด ์žˆ์Œ ์•„๋‹ˆ๋ฉด 0
      		int [] traveledPoint = new int[this.pointCount];
      		for(int i=0;i<pointCount;i++){
      			traveledPoint[i] = 0;
      		}
      		
      		for(int fromPoint=0;fromPoint<pointCount;fromPoint++){
      			for(int toPoint=0;toPoint<pointCount;toPoint++){
      				//์ž์‹ ๊ณผ ์ž์‹ ์˜ ๊ฒ€์‚ฌ์ด๋ผ๋ฉด skip
      				if(fromPoint==toPoint)
      					continue;
      				
      				//ํ•ด๋‹น ๋…ธ๋“œ๊ฐ€ mark ๋˜์–ด ์žˆ์œผ๋ฉด ๊ฒ€์‚ฌ ์ˆ˜ํ–‰
      				if(nodes[fromPoint][toPoint]){
      					//๋งŒ์•ฝ ์‹œ์ž‘์ ์˜ ์ƒ‰๊น”์ด ์•ˆ์น ํ•ด์ ธ ์žˆ์œผ๋ฉด  ๊ธฐ๋ณธ์ƒ‰์œผ๋กœ ์น ํ•จ
      					if(traveledPoint[fromPoint]==0){
      						traveledPoint[fromPoint] = RED;
      					}
      					//๋งŒ์•ฝ ์ข…๋ฃŒ์ ์˜ ์ƒ‰๊น”์ด ์•ˆ์น ํ•ด์ ธ์žˆ์œผ๋ฉด ์‹œ์ž‘์ ๊ณผ ๋ฐ˜๋Œ€์ƒ‰
      					if(traveledPoint[toPoint] ==0){
      						traveledPoint[toPoint] = traveledPoint[fromPoint] == RED? BLUE:RED;
      					}
      					
      					//์ข…๋ฃŒ์ ๊ณผ ์‹œ์ž‘์ ์˜ ์ƒ‰๊น”์ด ๊ฐ™์€์ง€ ๊ฒ€์‚ฌ ๋งŒ์ผ ๊ฐ™๋‹ค๋ฉด false๋ฆฌํ„ด
      					if(traveledPoint[fromPoint] == traveledPoint[toPoint]){
      						return false;
      					}
      					
      				}
      			}
      		}
      		
      		return true;
      	}
      
      }
    
  • ํ…Œ์ŠคํŠธ์ผ€์ด์Šค

      import junit.framework.TestCase;
      
      
      public class GraphTestCase extends TestCase {
      	public void testInitGraph(){
      		Graph graph = new Graph(3);
      		graph.insertNode(0,1);
      		graph.insertNode(1,2);
      		graph.insertNode(2,0);
      		assertFalse(graph.checkBio());
      	}
      	public void testInitGraph2(){
      		Graph graph = new Graph(7);
      		graph.insertNode(0,1);
      		graph.insertNode(0,2);
      		graph.insertNode(0,3);
      		graph.insertNode(0,4);
      		graph.insertNode(0,5);
      		graph.insertNode(0,6);
      		assertTrue(graph.checkBio());
      	}
      }
    
โš ๏ธ **GitHub.com Fallback** โš ๏ธ