Podcast Ch 24 b Title Graphs and Digraphs
Podcast Ch 24 b • Title: Graphs and Digraphs • Description: The Graph Interface; the Digraph class; Program 24. 1 • Participants: Barry Kurtz (instructor); John Helfert and Tobie Williams (students) • Textbook: Data Structures for Java; William H. Ford and William R. Topp
Creating and Using Graphs
Creating and Using Graphs (continued)
Creating and Using Graphs (continued)
Creating and Using Graphs (continued)
Creating and Using Graphs (continued)
Creating and Using Graphs (continued)
Student Question For a graph with n vertices, what is the minimum and maximum number of edges? Minimum: ______ Maximum: ______ For n = 5 draw the graph with the maximum number of edges
The Di. Graph Class • The Di. Graph class implements the Graph interface and adds other methods that are useful in applications. – A constructor creates an empty graph. – The methods in. Degree() and out. Degree() are special methods that access a properties that are unique to a digraph. – The static method read. Graph() builds a graph whose vertices are strings.
The Di. Graph Class (cont) • Di. Graph method read. Graph() inputs the vertex values and the edges from a textfile. – File format: (Number Source 1 Source 2. . . Sourcen of Edges n) Destination 1 Weight 1 Destination 2 Weight 2 Destinationn Weightn
The Di. Graph Class (cont) • The method to. String() provides a representation of a graph. For each vertex, the string gives the list of adjacent vertices along with the weight for the corresponding edge. The information for each vertex also includes its in-degree and out-degree.
The Di. Graph Class (cont) File samplegraph. dat 5 // data for the vertices A B C D E 6 // data for the edges A B 3 A C 2 B C 6 C B 4 C D 1 E B 5 // input vertices, edges, and weights from sample // graph. dat Di. Graph g = Di. Graph. read. Graph("samplegraph. dat"); // display the graph System. out. println(g)
The Di. Graph Class (cont) Output: A: in-degree 0 Edges: B(3) B: in-degree 3 Edges: C(6) C: in-degree 2 Edges: B(4) D: in-degree 1 Edges: E: in-degree 0 Edges: B(5) out-degree 2 C(2) out-degree 1 out-degree 2 D(1) out-degree 0 out-degree 1
Student Question Di. Graph<String> g; System. out. println(g); Run: A: in-degree 1 Edges: C(7) B: in-degree 0 Edges: C(9) C: in-degree 3 Edges: D: in-degree 2 Edges: E(3) E: in-degree 1 Edges: A(2) out-degree 2 D(5) out-degree 2 D(1) out-degree 0 out-degree 1 out-degree 2 C(6) Draw this graph
Student Question (a) In the pictured graph, identify a cycle with 3 or more vertices. (b) A directed acyclic graph (DAG) is a directed graph with no cycles. In the pictured graph, identify an edge which can be deleted so that the resulting graph is a DAG.
Program 24. 1
Program 24. 1 (continued) import java. io. File. Not. Found. Exception; ds. util. Set; ds. util. Iterator; ds. util. Di. Graph; public class Program 24_1 { public static void main(String[] args) throws File. Not. Found. Exception { // construct graph with vertices of type // String by reading from the file // "graph. IO. dat" Di. Graph<String> g = Di. Graph. read. Graph("graph. IO. dat"); String vtx. Name; // sets for vertex. Set() and adjacent // vertices (neighbors) Set<String> vtx. Set, neighbor. Set;
Program 24. 1 (continued) // output number of vertices and edges System. out. println("Number of vertices: " + g. number. Of. Vertices()); System. out. println("Number of edges: " + g. number. Of. Edges()); // properties relative to vertex A System. out. println("in. Degree for A: " + g. in. Degree("A")); System. out. println("out. Degree for A: " + g. out. Degree("A")); System. out. println("Weight e(A, B): " + g. get. Weight("A", "B")); // delete edge with weight 2 g. remove. Edge("B", "A"); // delete vertex "E" and edges (E, C), // (C, E) and (D, E) g. remove. Vertex("E");
Program 24. 1 (continued) /* add and update attributes of the graph */ // increase weight from 4 to 8 g. set. Weight("A", "B", 8); // add vertex F g. add. Vertex("F"); // add edge (F, D) with weight 3 g. add. Edge("F", "D", 3); // after all updates, output the graph // and its properties System. out. println("After all the graph updates"); System. out. println(g); // get the vertices as a Set and // create set iterator vtx. Set = g. vertex. Set(); Iterator vtx. Iter = vtx. Set. iterator();
Program 24. 1 (concluded) // scan the vertices and display // the set of neighbors while(vtx. Iter. has. Next()) { vtx. Name = (String)vtx. Iter. next(); neighbor. Set = g. get. Neighbors(vtx. Name); System. out. println(" Neighbor set for " + "vertex " + vtx. Name + " is " + neighbor. Set); } } }
Number of vertices: 5 Number of edges: 8 in. Degree for A: 1 out. Degree for A: 3 Weight e(A, B): 4 After all the graph updates A: in-degree 0 out-degree 3 Edges: B(8) C(7) D(6) B: in-degree 2 out-degree 0 Edges: C: in-degree 1 out-degree 1 Edges: B(3) D: in-degree 2 out-degree 0 Edges: F: in-degree 0 out-degree 1 Edges: D(3) Neighbor set for vertex D is [] Neighbor set for vertex F is [D] Neighbor set for vertex A is [D, B, C] Neighbor set for vertex B is [] Neighbor set for vertex C is [B] Program 24. 1 (Run)
Di. Graph<String> g; g. add. Vertex("G"); g. add. Edge("F", "G", 1); g. add. Edge("G", "D", 2); g. set. Weight("E", "D", 5); g. remove. Edge("D", "E"); g. remove. Vertex("C"); Original Graph Student Question Updated Graph
- Slides: 22