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.

Slides:



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

Review Binary Search Trees Operations on Binary Search Tree
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs Chapter 30 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
© 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.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Introduction to Graphs
Introduction to Graphs What is a Graph? Some Example applications of Graphs. Graph Terminologies. Representation of Graphs. –Adjacency Matrix. –Adjacency.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
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.
Graph Implementations Chapter 29 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Graphs Chapter 28 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
9.2 Graph Terminology and Special Types Graphs
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 25 Graph.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
Graphs Chapter 12.
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
Introduction to Graphs. Introduction Graphs are a generalization of trees –Nodes or verticies –Edges or arcs Two kinds of graphs –Directed –Undirected.
 What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
© 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.
Graphs 9.1 Graphs and Graph Models أ. زينب آل كاظم 1.
Graphs A graphs is an abstract representation of a set of objects, called vertices or nodes, where some pairs of the objects are connected by links, called.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
Homework #5 Due: October 31, 2000 Christine Kang Graph Concepts and Algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Discrete Structures CISC 2315 FALL 2010 Graphs & Trees.
Graphs Upon completion you will be able to:
Graphs. Contents Terminology Graphs as ADTs Applications of Graphs.
Chapter 9: Graphs.
Graphs Chapter 28 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
DATA STRUCTURE Presented By: Mahmoud Rafeek Alfarra Using C# MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
Lecture 20. Graphs and network models 1. Recap Binary search tree is a special binary tree which is designed to make the search of elements or keys in.
1 CSE 332: Graphs Richard Anderson Spring Announcements This week and next week – Graph Algorithms Reading, Monday and Wednesday, Weiss
رياضيات متقطعة لعلوم الحاسب MATH 226. Chapter 10.
Subject Four Graphs Data Structures. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes.
Podcast Ch24c Title: Breadth First Search
BCA-II Data Structure Using C Submitted By: Veenu Saini
Introduction to Graphs
Introduction to Graphs
Data Structures for Java William H. Ford William R. Topp
Graph Implementations
Graphs Graph transversals.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Copyright ©2012 by Pearson Education, Inc. All rights reserved
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Podcast Ch24b Title: Graphs and Digraphs
Graphs.
Data Structures & Algorithms
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Graphs Chapter 7 Visit for more Learning Resources.
Graph Implementation.
Graphs G = (V, E) V are the vertices; E are the edges.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Graphs G = (V, E) V are the vertices; E are the edges.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Introduction to Graphs
Introduction to Graphs
Presentation transcript:

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 vertices vi and vj. – A self-loop is an edge that connects a vertex to itself. We assume that none of our graphs have self-loops. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Vertices = {v 1, v 2, v 3, …, v m } Edges = {e 1, e 2, e 3, …, en }

Graph Terminology (continued) © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Graph Terminology (continued) The degree of a vertex is the number of edges originating at the vertex. Two vertices in a graph are adjacent (neighbors) if there is an edge connecting the vertices. A path between vertices v and w is a series of edges leading from v to w. The path length is the number of edges in the path. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Graph Terminology (continued) A path is simple if all its edges are distinct. A cycle is a simple path that starts and ends on the same vertex. A graph is connected if there is a path between any pair of distinct vertices. A complete graph is a connected graph in which each pair of vertices is linked by an edge. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Graph Terminology (continued) © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Graph Terminology (continued) A graph described until now is termed an undirected graph. Movement between vertices can occur in either direction. In a digraph, edges have a direction. There might be an edge from v to w but no edge from w to v. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Graph Terminology (continued) © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Graph Terminology (continued) In a digraph, a directed path (path) connecting vertices v s and v e is a sequence of directed edges that begin at v s and end at v e. The number of the edges that emanate from a vertex v is called the out-degree of the vertex. The number of the edges that terminate in vertex v is the in-degree of the vertex. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Graph Terminology (continued) A digraph is strongly connected if there is a path from any vertex to any other vertex. The digraph is weakly connected if, for each pair of vertices v i and v j, there is either a path P(v i, v j ) or a path P(v j,v i ). © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Graph Terminology (continued) © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Graph Terminology (concluded) An acyclic graph has no cycles. Each edge in a weighted digraph, has a cost associated with traversing the edge. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Creating and Using Graphs The Graph interface specifies all basic graph operations including inserting and erasing vertices and edges. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Creating and Using Graphs (continued) © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Creating and Using Graphs (continued) © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Creating and Using Graphs (continued) © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Creating and Using Graphs (continued) © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Creating and Using Graphs (continued) © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Creating and Using Graphs (continued) © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

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. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

The DiGraph Class (continued) DiGraph method readGraph() inputs the vertex values and the edges from a textfile. – File format: © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. (Number of Edges n) Source 1 Destination 1 Weight 1 Source 2 Destination 2 Weight 2... Source n Destination n Weight n

The DiGraph Class (continued) 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. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

The DiGraph Class (continued) © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. 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 samplegraph.dat DiGraph g = DiGraph.readGraph("samplegraph.dat"); // display the graph System.out.println(g)

The DiGraph Class (continued) © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. 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)

Program 24.1 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

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 g = DiGraph.readGraph("graphIO.dat"); String vtxName; // sets for vertexSet() and adjacent // vertices (neighbors) Set vtxSet, neighborSet;

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. 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");

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. 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();

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. 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); }