Solve The Graph

You might also like

Download as pdf
Download as pdf
You are on page 1of 9
sai2ri2020 _ww-mathes-emory.edurcheung/Courses/171/Syllabus/11-Graphbts him! ‘The Breadth First Search Graph traversal algorithm ‘+ Breadth First Search: visit the closest nodes first © Description of the Breadth First Search algorithm: = Start at some node (e.g,, node 0): * Visit lf the neighbors of node 0 firs: = Then vist the neighbors' neighbors: + And soon .www.mathes.emory edul-choung/Courses!171iSylabus!11-Geaphibis him! 19 sai2ri2020 _ww.mathes-emory.edur~cheung/Courses/171/Syllabus/11-Graphbts him! ‘+ Implementing the BFS algorithm © The BFS algorithm is implmented by: = Using a queue to store the nodes in the toVisitNodes data structure © Pseudo code: Set all nodes to "not visited"; @ = new Queue(); q-enqueue(initial node); while (q # enpty ) do ‘ x = q.dequeue(); if (x has not been visited ) visited[x] = true; // Nisit node x 1 for ( every edge (x, y) /* we are using all edges ! */ ) if (-y has not been visited ) q-enqueue(y); // Use the edge (x,y) 111 + Example of the BFS algorithm © Example: = Graph: 1 Initial state: node 0 is enguewed .www.mathes.emory edul-choung/Courses!171iSylabus!11-Geaphibis him! 219 _www.mathes-emory.educheung/Courses/171/Syllabus/11-Graphbts him! om al sai2ri2020 @ = enqueue = visited = State after visiting 0 bor nodes: 1,3, 8 Enqueue the unvisited n ‘Next, visit the first node in the quewe: 1 = State after visiting 1 ited neighbor nodes: 7 Enqueue the uni ‘Next, visit the first node in the queue: 3 = State after visiting 3 .www.mathes.emory edul-choung/Courses!171iSylabus!11-Geaphibis him! sai2ri2020 Enquewe the _www-mathes-emory.edu~cheung/Courses/171/Syllabus/11-Graphbts him! woe| ava | unvisited neighbor nodes: 2,4 Next, visit the fist node in the quewe: 8 Enqueue the out) 1 State after visiting 8 unvisited neighbor nodes: none (Note: 4is enqueued again, but won't be visited twice, so I leave it ‘Next, visit the first node in the quewe: 7 State after vi aT .www.mathes.emory edul-choung/Courses!171iSylabus!11-Geaphibis him! 49 sai2ri2020 out) _www-mathes-emory.edurcheung/Courses/171/Syllabus/11-Graphits him! Enqueue the unvisited neighbor nodes: none (Note: 2 is enqueued again, but won't be visited twice, so [leave it Next, visit the first node in the quewe: 2 = State after vi Enqueue the uni siting 2 ited neighbor nodes: 5 Next, visi the first node in the quewe: 4 1 State after vi e4 .www.mathes.emory edul-choung/Courses!171iSylabus!11-Geaphibis him! 59 sai2ri2020 _ww.mathes-emory.edur~cheung/Courses/171/Syllabus/11-Graphibts him! Enqueue the unvisited neighbor nodes: none ‘Next, visi the first node in the quewe: S = State after visiting 5 Enquewe the unvisit ‘Next, visit the first node in the quewe: 6 State after vi 26 .www.mathes.emory edul-choung/Courses!171iSylabus!11-Geaphibis him! cr sai2ri2020 _www.mathes-emory.edur~cheung/Courses/171/Syllabus/11-Graphbts him! DONE, (The queue has become empty) ‘+ Implementation in Java © Java code: public void BFS() { // BFS uses Queue data structure Queue q = new LinkedList(); // I use Queue class in Java's library| for (i = 0; i ¢ visited.length; i++) visited[i] = false; 11 Clear visited{] 4-a4d(0) 3 // Start the “to visit” at node @ ” Loop as long as there are "active" node = os " while( 1 q.isémpty() ) { int nextNode; I Next node to visit int 3 nexttiode = q-renove(); if ( 1 visited[nextNode] ) < visited[nextNode] = true; // Mark node as visited System.out.println("nextNode = " + nextNode ); for ( i= 0; i < NNodess ist ) if ( adjMatrix[nextNode][i] > @ && 1 visited[i] ) q-addca); } y i .www.mathes.emory edul-choung/Courses!171iSylabus!11-Geaphibis him! 719 sai2ri2020 _ww-mathes-emory.edurcheung/Courses/171/Syllabus/11-Graphbts him! © Test program using the graph given in this webpage: public static void main(string[] args) 7 e12345678 Graph G = new Graph(conn); 6.BFS()5 Output: Traversal order: © Example Program: 1» The BES Prog file: cick here = A Test program: click here How to run the program: 'k on link(s) and save ina .www.mathes.emory edul-choung/Courses!171iSylabus!11-Graphibis him! 89 sai2ri2020 _ww.mathes-emory.edurcheung/Courses/171/Syllabus/11-Graphbts hil ‘Tocompile: javac TestProg. java Torun: java TestProg .www.mathes.emory edul-choung/Courses!171iSylabus!11-Geaphibis him! a9

You might also like