Podcast Ch26a Title: Representing Graphs

Slides:



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

Topological Sort Topological sort is the list of vertices in the reverse order of their finishing times (post-order) of the depth-first search. Topological.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Data Structures Using C++
Data Structures Using Java1 Chapter 11 Graphs. Data Structures Using Java2 Chapter Objectives Learn about graphs Become familiar with the basic terminology.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Graphs. Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
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.
Graph Implementations Chapter 31 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
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.
1 Graphs & Characteristics Graph Representations A Representation in C++ (Ford & Topp) Searching (DFS & BFS) Connected Components Graph G and Its Transpose.
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.
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.
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.
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.
– Graphs 1 Graph Categories Strong Components Example of Digraph
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 20 Ordered.
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
Graphs – Part III CS 367 – Introduction to Data Structures.
Podcast Ch24c Title: Breadth First Search
BCA-II Data Structure Using C Submitted By: Veenu Saini
Podcast Ch23e Title: Implementing Huffman Compression
Podcast Ch17b Title: Iterative Tree Traversal
Podcast Ch24d Title: Depth First Search and Acyclic Graphs
Java Programming: Program Design Including Data Structures
CSC317 Shortest path algorithms
Podcast Ch17d Title: Drawing a Binary Tree
Podcast Ch17a Title: Expression Trees
CSE 373: Data Structures and Algorithms
Graphs Graph transversals.
Graphs Chapter 11 Objectives Upon completion you will be able to:
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Basic Graph Algorithms
Graph Operations And Representation
Podcast Ch25d Title: Minimum Path Algorithm
Podcast Ch18b Title: STree Class
Podcast Ch24b Title: Graphs and Digraphs
Graphs.
Data Structures & Algorithms
Podcast Ch22c Title: Deleting from a Heap
ITEC 2620M Introduction to Data Structures
Podcast Ch23b Title: BitArray Implementation
Podcast Ch18c Title: BST delete operation
Podcast Ch25c Title: Shortest Path Algorithm
Podcast Ch23f Title: Serialization
Text Book: Introduction to algorithms By C L R S
Podcast Ch22b Title: Inserting into a Heap
Graph Implementation.
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
Chapter 16 1 – Graphs Graph Categories Strong Components
Podcast Ch20b Title: TreeMap Design
Podcast Ch18d Title: Binary Search Tree Iterator
Podcast Ch21d Title: Hash Class Iterators
Podcast Ch26b Title: Digraph Class Implementation
Podcast Ch27a Title: Overview of AVL Trees
Podcast Ch21a Title: Hash Functions
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
Chapter 9 Graph algorithms
Podcast Ch23a Title: Bit Arrays
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

Podcast Ch26a Title: Representing Graphs Description: Adjacency matrix; adjacency list; VertexInfo class; vertex map; exploring the adjacency list Participants: Barry Kurtz (instructor); John Helfert and Tobie Williams (students) Textbook: Data Structures for Java; William H. Ford and William R. Topp

Representing Graphs Adjacency matrix representation of a graph uses a matrix whose entries give the weight of an edge (vi,vj) or 0 if there is no edge from vi to vj.

Draw the adjacency matrix for the following graph G.

Use the following digraph; a assume the weights are all 1. (a) Identify the adjacency matrix for the graph. (i) 0 1 1 0 1 (ii) 0 1 1 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 0 0 1 0 0 0 0 1 0 0 (iii) 0 1 0 0 0 (iv) 0 1 1 0 0 0 0 1 1 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 1

Representing Graphs (concluded) An Adjacency list representation of a graph includes the vertex with the list of its neighbors.

Give the adjacency list representation of the graph. Vertex Edges

Edge Class class Edge { // index of the destination vertex in the // ArrayList vInfo of vertex properties public int dest; // weight of this edge public int weight; public Edge(int dest, int weight) { this.dest = dest; this.weight = weight; } public boolean equals(Object obj) { return ((Edge)obj).dest == this.dest;

Representing Vertex Information The VertexInfo class has a reference to the vertex value, the in-degree of the vertex, a list of edges originating at the vertex and other information used by graph algorithms.

VertexInfo Class // maintains vertex properties, including // its set of Edges class VertexInfo<T> { // vertex reference back to the vertex in the map public T vertex; // list of Edge objects (adjacent vertices) // for the current vertex public LinkedList<Edge> edgeList; // maintains the in-degree of the vertex public int inDegree; // indicates whether the object currently // represents a vertex public boolean occupied;

VertexInfo Class (continued) // indicates vertex color for use in algorithms // that traverse the vertices of a Graph public VertexColor color; // available to algorithms for storing relevant // data values public int dataValue; // available to Graph algorithms; holds parent // which is a vertex that has an edge terminating // in the current vertex public T parent;

VertexInfo Class (concluded) // constructor creates an object with initial // values for the vertex, edgeList, inDegree, // and occupied fields public VertexInfo(T v) { vertex = v; edgeList = new LinkedList<Edge>(); inDegree = 0; occupied = true; }

VertexColor Class The VertexColor class maintains the color of a vertex. The colors of WHITE, GRAY, and BLACK are values of the enumerated type. public enum VertexColor { WHITE, GRAY, BLACK }

Vertex Map & VertexInfo ArrayList The vertex map contains key‑value pairs where the key is a reference to the vertex and the value is an index into an ArrayList of VertexInfo objects.

Vertex Map & VertexInfo ArrayList

For the following graph G, assume that the vInfo index of A: 0. B: 1 For the following graph G, assume that the vInfo index of A: 0 B: 1 C: 2 D: 3 Demonstrate the graph class representation for G in the map vtxMap and the array list vInfo containing a list of VertexInfo object. In vtxMap, fill-in the key and value components of the key-value pair. In vInfo, fill-in the in-degree and the edge set for each vertex. Recall that each entry of the edge list is of the form {dest vInfo index, weight}. For instance, the edge (A, C) is represented in the edge set of A by {2, 3}.

Exploring the Adjacency List A VertexInfo object allows access to all the neighbors of the vertex. VertexInfo<T> vtxInfo = vInfo.get(i); // declare an iterator for edgeList and position it // at the first element in the adjacency list Iterator<Edge> iter = vtxInfo.edgeList.iterator(); // extract the first element and output its weight Edge e = iter.next(); System.out.println(e.weight); // use the dest field to access the vInfo element // that corresponds to the adjacent vertex; the // index of the element is e.dest; // output the name of the vertex VertexInfo<T> edgeVtxInfo = vInfo.get(e.dest); System.out.println(edgeVtxInfo.vertex);

Exploring the Adjacency List of a Vertex (continued) Let us look at a code segment that scans the adjacency list looking for the edge of minimum weight. In order to output the edge, we must maintain the vInfo index of the destination vertex. // declare variables for the minimum dest index and // minimum weight int minDest, minWeight = Integer.MAX_VALUE; // VertexInfo objects for elements in vInfo; // current vertex element in vInfo is vtxInfo; // vInfo element for a neighbor is edgeVtxInfo VertexInfo<T> vtxInfo = null, edgeVtxInfo = null; ...

Exploring the Adjacency List of a Vertex (continued) // iterator scans adjacency list for current vertex Iterator<Edge> iter = vtxInfo.edgeList.iterator(); while (iter.hasNext) { // extract next element (Edge object) from // adjacency list Edge e = iter.next(); // update if new minimum weight is discovered if (e.weight < minWeight) minWeight = e.weight; minDest = e.dest; }

Exploring the Adjacency List of a Vertex (continued) // output the edge that has minimum weight; // first get the corresponding vInfo element // and then access the vertex field edgeVtxInfo = vInfo.get(minDest); System.out.println("Edge (" + vtxInfo.vertex + ", " + edgeVtxInfo.vertex + ") has weight + edgeVtxInfo.weight);

The occupied Field Occupied field of a VertexInfo object indicates if the object corresponds to an existing vertex entry in the map. for (i = 0; i < vInfo.size(); i++) { VertexInfo<T> vtxInfo = vInfo.get(i); if (vtxInfo.occupied) < vInfo element corresponds to an actual vertex> }