[질문] 그래프의 사이클 탐지하는 recusive 함수를 만들었는데 안되네요...

tyolee83의 이미지

directed graph의 cycle을 탐지하는 함수를 만들어야 하는데요

사이클인지 아닌지는 이렇게 호출합니다.

//cycle check
if(checkCycle(connectedVertex[0],connectedVertex[1])){
	//cycle detected!!
	System.out.println("!CYCLE!!!");
}

동작 방식은 일단 에지를 그어보고 그게 사이클을 만들게 되면 빼는 방식입니다.
connectedVertex[0]은 현재 그으려는 에지의 출발점, connectedVertex[1]은 도착점입니다. [0]->[1] 이렇게 긋는 거구요

다음으로 recursive 함수를 이렇게 디자인하였습니다.

private boolean checkCycle(Vertex checkVertex, Vertex currentVertex){
		if(currentVertex.getEdgeList().size()==0)
			return false;
		boolean ret = false;	
 
		for(Edge e : currentVertex.getEdgeList()){
			if(e.getEndpoints()[1].equals(checkVertex)){
				ret = true;
				break;
			}
			if(checkCycle(checkVertex,e.getEndpoints()[1])){
				ret = true;
				break;
			}
		}		
		return ret;
	}

getEndpoints[0]은 시작점, [1]은 도착점을 나타내구요~
즉, 맨처음 vertex를 계속 인자로 주고, recursive하게 edge를 따라가서 그 에지가 결국 맨처음 vertex와 같으면!
cycle이다!!
이렇게 디자인한건데요

문제는 사이클 뿐만 아니라 모든 커넥션을 맺을 때 스택 오버플로가 납니다.

recursive 디자인이 잘못된것 같은데, 도움좀 부탁드립니다.

totohero의 이미지

이미 방문한 node를 check 대상에서 제외하기 위한 장치가 없나요? 0 -> 1 -> 2 와 같이 연결되어 있고 2 -> 1가 다시 연결된 경우라면 무한 recursion이 발생할 것 같군요. 양방향 edge가 없다고 가정해도 전체 node/edge 수를 크게 상회하는 recursion이 발생할 수 있고 스택이 모자랄 수 있겠죠. visted node와 그렇지 않은 node들을 나타내는 set을 만들고 recursion대신 iteration을 사용하는 것이 효율적이지 않을까요?

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.