Podcast Ch24b Title: Graphs and Digraphs

Slides:



Advertisements
Similar presentations
CSE Lectures 18 – Graphs Graphs & Characteristics
Advertisements

Chapter 9: Graphs Topological Sort
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
CSE 2331/5331 Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 24 Graphs.
Algoritma Traversal Graph. Graph Traversal Algorithms In general, graphs do not have a vertex, like a root, that initiates unique paths to each of the.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices. Edges are.
CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms.
Graph Operations And Representation. Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
ADSA: Maps/ Advanced Data Structures and Algorithms Objectives – –examples of maps, and introduce map collection views Semester 2,
Introduction to Graphs. Introduction Graphs are a generalization of trees –Nodes or verticies –Edges or arcs Two kinds of graphs –Directed –Undirected.
Graph. Terminologi Graph A graph consists of a set of vertices V, along with a set of edges E that connect pairs of vertices. – An edge e = (vi,vj) connects.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 8 Collection.
Data Structures & Algorithms Graphs. Graph Terminology A graph consists of a set of vertices V, along with a set of edges E that connect pairs of vertices.
COSC 2007 Data Structures II Chapter 14 Graphs I.
Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Graphs 황승원 Fall 2010 CSE, POSTECH. 2 2 Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects.
DIRECTED ACYCLIC GRAPHS AND TOPOLOGICAL SORT CS16: Introduction to Data Structures & Algorithms Tuesday, March 10,
CSC2100B Tutorial 10 Graph Jianye Hao.
Graphs Upon completion you will be able to:
abstract data types built on other ADTs
Podcast Ch24c Title: Breadth First Search
Directed Graphs 12/7/2017 7:15 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Podcast Ch23e Title: Implementing Huffman Compression
Podcast Ch17b Title: Iterative Tree Traversal
Topological Sorting.
Podcast Ch24d Title: Depth First Search and Acyclic Graphs
Podcast Ch26a Title: Representing Graphs
Basic Concepts Graphs For more notes and topics visit:
Podcast Ch17d Title: Drawing a Binary Tree
Podcast Ch17a Title: Expression Trees
Directed Graphs 9/20/2018 1:45 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
CS120 Graphs.
More Graph Algorithms.
Data Structures for Java William H. Ford William R. Topp
Graphs Graph transversals.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Topological Sort CSE 373 Data Structures Lecture 19.
Walks, Paths, and Circuits
Podcast Ch25d Title: Minimum Path Algorithm
Podcast Ch18b Title: STree Class
CSE 373 Graphs 4: Topological Sort reading: Weiss Ch. 9
Data Structures & Algorithms
Podcast Ch22c Title: Deleting from a Heap
CSCI2100 Data Structures Tutorial
Podcast Ch23b Title: BitArray Implementation
Podcast Ch18c Title: BST delete operation
Podcast Ch25c Title: Shortest Path Algorithm
Podcast Ch23f Title: Serialization
Podcast Ch22b Title: Inserting into a Heap
Graphs G = (V, E) V are the vertices; E are the edges.
Podcast Ch18a Title: Overview of Binary Search Trees
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Podcast Ch20b Title: TreeMap Design
Podcast Ch18d Title: Binary Search Tree Iterator
Podcast Ch21d Title: Hash Class Iterators
CSE 417: Algorithms and Computational Complexity
Podcast Ch26b Title: Digraph Class Implementation
Podcast Ch27a Title: Overview of AVL Trees
Podcast Ch21f Title: HashSet Class
Podcast Ch23d Title: Huffman Compression
Podcast Ch27b Title: AVLTree implementation
Podcast Ch22a Title: Array-based Binary Trees
Podcast Ch24b Title: Strongly Connected Components
Podcast Ch21b Title: Collision Resolution
Graphs G = (V,E) V is the vertex set.
Podcast Ch23a Title: Bit Arrays
Podcast Ch23c Title: Binary Files
Presentation transcript:

Podcast Ch24b 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 DiGraph Class The DiGraph class implements the Graph interface and adds other methods that are useful in applications. A constructor creates an empty graph. The methods inDegree() and outDegree() are special methods that access a properties that are unique to a digraph. The static method readGraph() builds a graph whose vertices are strings.

The DiGraph Class (cont) DiGraph method readGraph() inputs the vertex values and the edges from a textfile. File format: (Number of Edges n) Source1 Destination1 Weight1 Source2 Destination2 Weight2 . . . Sourcen Destinationn Weightn

The DiGraph Class (cont) The method toString() 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 DiGraph 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 DiGraph g = DiGraph.readGraph("samplegraph.dat"); // display the graph System.out.println(g)

The DiGraph Class (cont) Output: A: in-degree 0 out-degree 2 Edges: B(3) C(2) B: in-degree 3 out-degree 1 Edges: C(6) C: in-degree 2 out-degree 2 Edges: B(4) D(1) D: in-degree 1 out-degree 0 Edges: E: in-degree 0 out-degree 1 Edges: B(5)

Student Question DiGraph<String> g; System.out.println(g); Run: A: in-degree 1 out-degree 2 Edges: C(7) D(5) B: in-degree 0 out-degree 2 Edges: C(9) D(1) C: in-degree 3 out-degree 0 Edges: D: in-degree 2 out-degree 1 Edges: E(3) E: in-degree 1 out-degree 2 Edges: A(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.FileNotFoundException; import ds.util.Set; import ds.util.Iterator; import ds.util.DiGraph; public class Program24_1 { public static void main(String[] args) throws FileNotFoundException { // construct graph with vertices of type // String by reading from the file // "graphIO.dat" DiGraph<String> g = DiGraph.readGraph("graphIO.dat"); String vtxName; // sets for vertexSet() and adjacent // vertices (neighbors) Set<String> vtxSet, neighborSet;

Program 24.1 (continued) // output number of vertices and edges System.out.println("Number of vertices: " + g.numberOfVertices()); System.out.println("Number of edges: " + g.numberOfEdges()); // properties relative to vertex A System.out.println("inDegree for A: " + g.inDegree("A")); System.out.println("outDegree for A: " + g.outDegree("A")); System.out.println("Weight e(A,B): " + g.getWeight("A","B")); // delete edge with weight 2 g.removeEdge("B", "A"); // delete vertex "E" and edges (E,C), // (C,E) and (D,E) g.removeVertex("E");

Program 24.1 (continued) /* add and update attributes of the graph */ // increase weight from 4 to 8 g.setWeight("A","B",8); // add vertex F g.addVertex("F"); // add edge (F,D) with weight 3 g.addEdge("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 vtxSet = g.vertexSet(); Iterator vtxIter = vtxSet.iterator();

Program 24.1 (concluded) // scan the vertices and display // the set of neighbors while(vtxIter.hasNext()) { vtxName = (String)vtxIter.next(); neighborSet = g.getNeighbors(vtxName); System.out.println(" Neighbor set for " + "vertex " + vtxName + " is " + neighborSet); }

Program 24.1 (Run) Number of vertices: 5 Number of edges: 8 inDegree for A: 1 outDegree 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 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)

Student Question DiGraph<String> g; g.addVertex("G"); g.addEdge("F","G",1); g.addEdge("G","D",2); g.setWeight("E","D", 5); g.removeEdge("D","E"); g.removeVertex("C"); Original Graph Updated Graph