Graph. Data Structures Linear data structures: –Array, linked list, stack, queue Non linear data structures: –Tree, binary tree, graph and digraph.

Slides:



Advertisements
Similar presentations
Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
Advertisements

PSU CS Algorithms Analysis and Design Graphs.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Data Structures Using C++
 Graph Graph  Types of Graphs Types of Graphs  Data Structures to Store Graphs Data Structures to Store Graphs  Graph Definitions Graph Definitions.
Graph.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
1 Representing Graphs. 2 Adjacency Matrix Suppose we have a graph G with n nodes. The adjacency matrix is the n x n matrix A=[a ij ] with: a ij = 1 if.
Chapter 9 Graph algorithms. Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Introduction to Graphs
Introduction to Graphs What is a Graph? Some Example applications of Graphs. Graph Terminologies. Representation of Graphs. –Adjacency Matrix. –Adjacency.
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.
Balanced Binary Search Trees height is O(log n), where n is the number of elements in the tree AVL (Adelson-Velsky and Landis) trees red-black trees get,
Graph Operations And Representation. Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
ALG0183 Algorithms & Data Structures Lecture 18 The basics of graphs. 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks.
GRAPHS Education is what remains after one has forgotten what one has learned in school. Albert Einstein Albert Einstein Smitha N Pai.
Graphs Algorithm Design and Analysis Week 4 Bibliography: [CLRS]- Subchap 22.1 – Representation of Graphs.
Graphs CS /02/05 Graphs Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Definition.
Graphs. Motivating Problem Konigsberg bridge problem (1736): Konigsberg bridge problem (1736): C A B D ab c d e f g Starting in one land area, is it possible.
1 Abstract Data Type We have discussed: List Tree Today we will talk about Graph.
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-1 Chapter 6 Graphs Introduction to Data Structure CHAPTER 6 GRAPHS 6.1 The Graph Abstract Data Type.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 2 Graph Algorithms.
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
Graphs Chapter 12.
 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.
1 CS104 : Discrete Structures Chapter V Graph Theory.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
CSE373: Data Structures & Algorithms Lecture 15: Introduction to Graphs Nicki Dell Spring 2014.
Data Structures & Algorithms Graphs
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.
Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
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.
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. Graph Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected,
Graph. 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.
Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems.
Graphs Upon completion you will be able to:
Copyright © Curt Hill Graphs Definitions and Implementations.
Graphs and Paths : Chapter 15 Saurav Karmakar
Introduction to Graph Theory By: Arun Kumar (Asst. Professor) (Asst. Professor)
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Graphs Definition: a graph is an abstract representation of a set of objects where some pairs of the objects are connected by links. The interconnected.
Chapter 05 Introduction to Graph And Search Algorithms.
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 Data Structures and Algorithms Graphs. 2 Graphs Basic Definitions Paths and Cycles Connectivity Other Properties Representation Examples of Graph Algorithms:
1 CSE 332: Graphs Richard Anderson Spring Announcements This week and next week – Graph Algorithms Reading, Monday and Wednesday, Weiss
Balanced Binary Search Trees
Introduction to Graphs
Introduction to Graphs
Unit 3 Graphs.
CS120 Graphs.
Balanced Binary Search Trees
Graph Operations And Representation
Graph Algorithm.
Graph Operations And Representation
CSCI2100 Data Structures Tutorial
Graphs Chapter 7 Visit for more Learning Resources.
Graphs G = (V, E) V are the vertices; E are the edges.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Graph Operations And Representation
Graphs G = (V,E) V is the vertex set.
Chapter 9 Graph algorithms
Heaps Chapter 6 Section 6.9.
Introduction to Graphs
Presentation transcript:

Graph

Data Structures Linear data structures: –Array, linked list, stack, queue Non linear data structures: –Tree, binary tree, graph and digraph

Graph A graph is a non-linear structure (also called a network) Unlike a tree or binary tree, a graph does not have a root Any node (vertex, v) can be connected to any other node by an edge (e) Can have any number of edges (E) and nodes (set of Vertex V) Application: the highway system connecting cities on a map a graph data structure

Graphs G = (V,E) V is the vertex set (nodes set). Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices. Edges are also called arcs and lines. Directed edge has an orientation (u,v). uv

Graphs Undirected edge has no orientation (u,v). uv Undirected graph => no oriented edge. Directed graph => every edge has an orientation. uv

Undirected Graph G = (V, E) V = {1,2,…11} E = { (2,1), (4,1), …}

Directed Graph (Digraph) G = (V, E) V = {1,2,…11} E = { (1,2), (1,4), …}

Applications—Communication Network Vertex = city, edge = communication link

Driving Distance/Time Map Vertex = city, edge weight = driving distance/time. Find a shortest path from city 1 to city 5 –1  2  5 (12) –1  4  6  7  5 (14)

Street Map Some streets are one way. Find a path from 1 

Complete Undirected Graph Has all possible edges. n = 1 n = 2 n = 3 n = 4

Number Of Edges—Undirected Graph Each edge is of the form (u,v), u != v. Number of such pairs in an n vertex graph is n(n-1). Since edge (u,v) is the same as edge (v,u), the number of edges in a complete undirected graph is n(n-1)/2. Number of edges in an undirected graph is <= n(n-1)/2.

Complete directed Graph Has all possible edges. n = 1 n = 2 n = 3 n = 4

Number Of Edges—Directed Graph Each edge is of the form (u,v), u != v. Number of such pairs in an n vertex graph is n(n-1). Since edge (u,v) is not the same as edge (v,u), the number of edges in a complete directed graph is n(n-1). Number of edges in a directed graph is <= n(n-1).

Vertex Degree Number of edges incident to vertex. degree(2) = 2, degree(5) = 3, degree(3) =

Sum Of Vertex Degrees Sum of degrees = 2e (e is number of edges)

In-Degree Of A Vertex in-degree is number of incoming edges indegree(2) = 1, indegree(8) =

Out-Degree Of A Vertex out-degree is number of outbound edges outdegree(2) = 1, outdegree(8) =

Sum Of In- And Out-Degrees each edge contributes 1 to the in-degree of some vertex and 1 to the out-degree of some other vertex sum of in-degrees = sum of out-degrees = e, where e is the number of edges in the digraph

Graph Operations And Representation

Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.

Path Finding Path between 1 and Path length is 20.

Another Path Between 1 and Path length is 28.

Example Of No Path No path between 2 and

Connected Graph Undirected graph. There is a path between every pair of vertices.

Example Of Not Connected

Connected Graph Example

Connected Components

Connected Component A maximal subgraph that is connected. A connected graph has exactly 1 component.

Not A Component

Communication Network Each edge is a link that can be constructed

Communication Network Problems Is the network connected?  Can we communicate between every pair of cities? Find the components. Want to construct smallest number of links so that resulting network is connected (finding a minimum spanning tree).

Cycles And Connectedness Removal of an edge that is on a cycle does not affect connectedness.

Cycles And Connectedness Connected subgraph with all vertices and minimum number of edges has no cycles.

Tree Connected graph that has no cycles. n vertex connected graph with n-1 edges.

Graph Representation Adjacency Matrix Adjacency Lists  Linked Adjacency Lists  Array Adjacency Lists

Adjacency Matrix 0/1 n x n matrix, where n = # of vertices A(i,j) = 1 iff (i,j) is an edge

Adjacency Matrix Properties Diagonal entries are zero. Adjacency matrix of an undirected graph is symmetric.  A(i,j) = A(j,i) for all i and j.

Adjacency Matrix (Digraph) Diagonal entries are zero. Adjacency matrix of a digraph need not be symmetric.

Adjacency Matrix n 2 bits of space For an undirected graph, may store only lower or upper triangle (exclude diagonal).  (n-1)n/2 bits

Adjacency Lists Adjacency list for vertex i is a linear list of vertices adjacent from vertex i. An array of n adjacency lists aList[1] = (2,4) aList[2] = (1,5) aList[3] = (5) aList[4] = (5,1) aList[5] = (2,4,3)

Linked Adjacency Lists Each adjacency list is a chain aList[1] aList[5] [2] [3] [4] Array Length = n # of chain nodes = 2e (undirected graph) # of chain nodes = e (digraph)

Array Adjacency Lists Each adjacency list is an array list aList[1] aList[5] [2] [3] [4] Array Length = n # of list elements = 2e (undirected graph) # of list elements = e (digraph)

Number Of C++ Classes Needed Graph representations  Adjacency Matrix  Adjacency Lists  Linked Adjacency Lists  Array Adjacency Lists  3 representations Graph types  Directed and undirected.  Weighted and unweighted.  2 x 2 = 4 graph types 3 x 4 = 12 C++ classes

Abstract Class Graph template class graph { public: // ADT methods come here // implementation independent methods come here };

Abstract Methods Of Graph // ADT methods virtual ~graph() {} virtual int numberOfVertices() const = 0; virtual int numberOfEdges() const = 0; virtual bool existsEdge(int, int) const = 0; virtual void insertEdge(edge *) = 0; virtual void eraseEdge(int, int) = 0; virtual int degree(int) const = 0; virtual int inDegree(int) const = 0; virtual int outDegree(int) const = 0;

Abstract Methods Of Graph // ADT methods (continued) virtual bool directed() const = 0; virtual bool weighted() const = 0; virtual vertexIterator * iterator(int) = 0; virtual void output(ostream&) const = 0;