1 / 12

Graph Algorithm

Graph Algorithm. Zhen Jiang West Chester University zjiang@wcupa.edu. Outline. Graph table, p362 Depth first search, p399 Width first search Shortest path and Dijkstra ’ s algorithm, p366. Graph table for undirectional network. Backbone list, to store all nodes. NULL. 1. 2. 3. 4.

burnst
Download Presentation

Graph Algorithm

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Graph Algorithm Zhen Jiang West Chester University zjiang@wcupa.edu

  2. Outline • Graph table, p362 • Depth first search, p399 • Width first search • Shortest path and Dijkstra’s algorithm, p366

  3. Graph table for undirectional network Backbone list, to store all nodes NULL 1 2 3 4 head 1 2 1 1 2 3 3 2 3 2 4 Neighbor list, to store all neighbors 4

  4. Graph table for directional network Backbone list, to store all nodes NULL 1 2 3 4 head 1 2 4 2 3 3 2 Neighbor list, to store all neighbors 4

  5. public class Edge{private String p;private String v;private double weight;public Edge(String a, String b){ p = a; v = b; weight = 1;}public Edge(String a, String b, double w){ p = a; v = b; weight = w;}public String getDest(){ return v;}public double getWeight(){ return weight;}public String getSource() { return p;}}// end of Edge class

  6. public class Node{private String source;private ArrayList<Edge> neighbors;public Node(String a){source = a;neighbors = new ArrayList<Edge>();}public Node(String a, ArrayList<Edge> b){source = a;neighbors = b;}public void addEdge(Edge x){neighbors.add(x);}public String getSource(){return source;}public int getNnum(){return neighbors.size();}public Edge getNeighbor(int i){return neighbors.get(i);}public boolean equalsV(String s){return source.equals(s);}public String toString(){… }}

  7. public class Graph{private ArrayList<Node> head = new ArrayList<Node>(); public Graph(String filename, String delimiter){ … } public String toString(){String ret ="";for(int i = 0; i<head.size(); i++){ Node tmp = head.get(i); ret += tmp.toString();}return ret;} }

  8. Depth First Search • Step 0: Push node 1 to SL, select node 1 as node u. • Step 1: If node u is not visited (not found in ST), save the value of node u in print-out string (if u is the destination, then the search stops), and push u to ST. • Step 2: Find a node v in link list of node u which is not visited yet (not in ST). • Step 3.1: If node v is found, push v into SL. Select v as u. • Step 3.2: Otherwise (in case not found), pop out (u from) SL. Select top node of SL as node u. • Step 4: Repeat the steps 1 to 4 until SL is empty.

  9. Width First Search • Step 0: When node 1 <> destination, offer node 1 to QL (from the tail) and QT (Stack or ArrayList). • Step 1: Poll out the node in QL (from the head) as node u. • Step 2: Save the value of node u in printout String. • Step 3: Offer all the neighbors of node u which are not in QT to (the tail of) QL. Save them to QT as well. (if the destination is one of them, the search stops.) • Step 4: Repeat the steps 1 to 4 until QL is empty. • Suggest to use ArrayList for both Qs.

  10. 103 104 public String width_search(String s){105 return width_search(s, null);106 }107 public String width_search(String s, String t){108 if(s==null||contains(s)<0) {109 throw new NoSuchElementException("Start vertex not found in width first search");110 } 111 // return spanning with search result attached.112 // if t != null, return the path (or entire spanning tree) 113 // to reach it.114 if(s.equals(t)) return "reached";115 ArrayList<Node> QL = new ArrayList<Node>(); 116 ArrayList<Node> QT = new ArrayList<Node>(); 117 String ret=""; //StringBuilder118 Node u = head.get(contains(s));119 QL.add(u);120 QT.add(u);121 while(QL.size()>0){122 Edge tmp;123 Node t2=null;124 u = QL.get(0);125 QL.remove(u);126 ret += u.getSource() + "=>";127 for(int i=0; i<u.getNnum(); i++){ 128 tmp = u.getNeighbor(i);129 t2 = head.get(contains(tmp.getDest()));130 if (!QT.contains(t2)){131 QT.add(t2);132 QL.add(t2);133 if(t2.equalsV(t)) return ret+tmp.getDest()+" (reached)"; 134 }135 }// end of for136 }137 return ret;138 }

  11. Shortest path • Step 0: Suggest to use ArrayList to realize the following “Stack.” • Step 1: current node c= s, c.length = 0, stack.push (c, its length, and parent). If c is the source s then no need for parent. if s == d, return the current stack. • Step 2: min = 0, hop = , index = 0 // zero-based index • Step 3: pick up the index’th record in stack, say node u, its length u.length, and its parent w. • Step 4: find a neighbor of u in the table of neighbors, say v, such that v is not found in any item in stack and <u,v> +u.length< hop. • Step 5: if such a neighbor is found, hop=min=u.length + <u,v> and record_node = v (record_parent = u). • Step 6: go to step 4 until all the neighbors of u have been tried (all can be found in stack).

  12. Step 7: index ++, go to step 3 until all the nodes have been tried (found in stack). • Step 8: c = record_node, c.length = min, stack_push(c, c.length, record_parent). If c = = d stop the entire process and goes to step 9 for data collection, otherwise go to step 2. • Step 9: (t, d.length, and t.parent) = (d, d.length, and d.parent) in stack, keep searching on (t.parent, t.parent.length, t.parent.parent), until t.parent = s.

More Related