1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<전체 코드>
 
import java.util.*;
 
 
 
public class Solution {
 
 
 
    public static void BFS(int v,int[][] graph) {
 
//순서 : 큐에 넣으면서 바로바로 방문해준다. if문으로 방문조건을 확인해준다.
 
        boolean[] isV=new boolean[graph.length];
 
        Queue queue;
 
        queue = new LinkedList<Integer>();
 
        
 
        isV[v-1]=true;
 
 
 
        System.out.print(v+ " ");
 
 
 
        queue.add(v);
 
        while(!queue.isEmpty()) {
 
            v=(int)queue.poll();
 
            for(int i=1;i<=graph.length;i++) {
 
                if(graph[v-1][i-1]==1&&!isV[i-1]) {
 
                    System.out.print(i+" ");
 
                    queue.add(i);
 
                    isV[i-1]=true;
 
                }
 
            }
 
        }
 
    }
 
 
 
     public static void DFS_stack(int v,int[][] graph) {
 
          //(1)stack 생성
 
          Stack<Integer>sstack = new Stack<Integer>();
 
          //(2)isVisited 생성 및 초기화.
 
          boolean[] isVisited=new boolean[graph.length];
 
          for(int i=0;i<graph.length;i++)
 
             isVisited[i]=false;
 
                    
 
          sstack.push(v-1);
 
          while(!sstack.isEmpty()) {
 
             v = sstack.pop();
 
 
 
             if(isVisited[v]==false// 방문 안한곳에서 출발
 
             {
 
                System.out.print((v+1)+" ");
 
                isVisited[v]=true;
 
                for(int i=graph.length-1;i>=0;i--) {//끝부터 해야 순서가 맞는다.
 
                   if(graph[v][i]==1&&!isVisited[i]) {//[ -1]인지 아닌지,잘 파악해야한다.
 
                         sstack.push(i); // 넣어주기만 한다.
 
                   }
 
                }
 
             }
 
          }
 
       }
 
    
 
    public static void main(String[] args) {
 
        Scanner scan = new Scanner(System.in);
 
        
 
        int m=scan.nextInt();
 
        int n=scan.nextInt();
 
        int v=scan.nextInt();
 
        
 
        int [][]graph=new int[m][m];
 
        for(int i=0;i<m;i++) {
 
            for(int j=0;j<m;j++) graph[i][j]=0;
 
        }
 
        
 
        for(int i=0;i<n;i++) {
 
            int start=scan.nextInt();
 
            int end=scan.nextInt();
 
            graph[start-1][end-1]=1;
 
            graph[end-1][start-1]=1;
 
        }
 
/*
        for(int i=0;i<m;i++) {
            for(int j=0;j<m;j++) System.out.print(graph[i][j]+" ");
            System.out.println();
        }*/
 
        DFS_stack(v,graph);
 
        System.out.println();
 
        BFS(v,graph);
 
    }
 
 
 
}
cs

'알고리즘 > 그래프' 카테고리의 다른 글

[DFS]백준_로봇청소기  (0) 2019.04.10
[완탐/BFS]백준_연구소  (0) 2019.04.08
[DFS및예외처리]백준_테트로미노  (0) 2019.04.07
[그래프BFS]백준1012유기농배추  (0) 2019.03.10
[그래프BFS]토마토 백준7576  (0) 2019.03.08

+ Recent posts