TIL : 210618_금_(그래프:인접행렬만들기)

beablessing·2021년 6월 18일
0

TIL

목록 보기
22/33
post-thumbnail

인접행렬 만들기

  • vertex 추가하는 메소드 만들기
///////////////첫번째 방법////////////
class GraphWithAdjacencyMatrix{
	constructor(){
    	this.matrix = [];
    }
  	const currentLength = this.matrix.length; 
	for(let i =0; i<this.matrix.length; i++){
    	this.matrix[i].push(0);
    }
	this.matrix.push(new Array(currentLength+1).fill(0));
}


///////////////두번째 방법////////////
  /*
edges = [
	[0, 3, "directed"],
	[0, 2, "directed"],
	[1, 3, "directed"],
	[2, 1, "directed"],
]
 */

function createMatrix(edges){
	let max = 0; // 각 요소 중 최대값!!!
	const matrix = new Array(max+1).fill(0).map((el) =>{  //[0,0,0,0]
    	return new Array(max+1).fill(0) ; // 4*4 의 모양새를 갖춤 
    })

}
  • vertex 존재하는지 확인하는 메소드 만들기
profile
프론트엔드 개발자

0개의 댓글