Introduction to Graphs

Slides:



Advertisements
Similar presentations
22C:19 Discrete Math Graphs Fall 2010 Sukumar Ghosh.
Advertisements

22C:19 Discrete Math Graphs Fall 2014 Sukumar Ghosh.
Review Binary Search Trees Operations on Binary Search Tree
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Chapter 8, Part I Graph Algorithms.
Introduction This chapter explores graphs and their applications in computer science This chapter explores graphs and their applications in computer science.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
 Graph Graph  Types of Graphs Types of Graphs  Data Structures to Store Graphs Data Structures to Store Graphs  Graph Definitions Graph Definitions.
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.
Applied Discrete Mathematics Week 12: Trees
Graph & BFS.
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 What is a Graph? Some Example applications of Graphs. Graph Terminologies. Representation of Graphs. –Adjacency Matrix. –Adjacency.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
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.
1 Section 8.4 Connectivity. 2 Paths In an undirected graph, a path of length n from u to v, where n is a positive integer, is a sequence of edges e 1,
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
GRAPHS Education is what remains after one has forgotten what one has learned in school. Albert Einstein Albert Einstein Smitha N Pai.
GRAPH Learning Outcomes Students should be able to:
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
Computer Science 112 Fundamentals of Programming II Introduction to Graphs.
Graph Theoretic Concepts. What is a graph? A set of vertices (or nodes) linked by edges Mathematically, we often write G = (V,E)  V: set of vertices,
U n i v e r s i t y o f H a i l ICS 202  2011 spring  Data Structures and Algorithms  1.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
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.
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.
Based on slides by Y. Peng University of Maryland
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs.  Definition A simple graph G= (V, E) consists of vertices, V, a nonempty set of vertices, and E, a set of unordered pairs of distinct elements.
COSC 2007 Data Structures II Chapter 14 Graphs I.
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
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++
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
GRAPHS. Graph Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected,
© 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:
Chapter 9: Graphs.
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.
Chapter 05 Introduction to Graph And Search Algorithms.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
1 GRAPH Learning Outcomes Students should be able to: Explain basic terminology of a graph Identify Euler and Hamiltonian cycle Represent graphs using.
رياضيات متقطعة لعلوم الحاسب 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.
Unit 10 Graphs (1) King Fahd University of Petroleum & Minerals
Introduction to Graphs
Introduction to Graphs
Graphs Chapter 13.
Graphs All tree structures are hierarchical. This means that each node can only have one parent node. Trees can be used to store data which has a definite.
Graphs Chapter 11 Objectives Upon completion you will be able to:
CS100: Discrete structures
Graph Operations And Representation
Graphs.
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Graphs G = (V, E) V are the vertices; E are the edges.
Chapter 9 Graph algorithms
Introduction to Graphs
Introduction to Graphs
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:

Introduction to Graphs What is a Graph? Some Example applications of Graphs. Graph Terminologies. Representation of Graphs. Adjacency Matrix. Adjacency Lists. Simple Lists Review Questions.

What is a Graph? Weighted Undirected Directed (Digraph) Graphs are Generalization of Trees. A simple graph G = (V, E) consists of a non-empty set V, whose members are called the vertices of G, and a set E of pairs of distinct vertices from V, called the edges of G. A simple graph has no loop (An edge that connects a vertex to itself) A simple graph has no vertices joined by multiple edges Example: A graph with a loop: Weighted Undirected Directed (Digraph)

Some Example Applications of Graphs Finding the least congested route between two phones, given connections between switching stations. Determining if there is a way to get from one page to another, just by following links. Finding the shortest path from one city to another. As a traveling sales-person, finding the cheapest path that passes through all the cities that the sales person must visit. Determining an ordering of courses so that prerequisite courses are always taken first. Networks:Vertices represent computers and edges represent network connectionsbetween them. World Wide Web: Vertices represent webpages, and edges represent hyperlinks.

Graphs Terminologies Cycle Disconnected Path Connected Adjacent Vertex: A vertex W is adjacent to a vertex V if there is an edge from V to W Example: B is adjacent to A; but A is not adjacent to B A path: A path is a sequence of consecutive edges in a graph. The length of the path is the number of edges traversed. A cycle (or circuit): a subset of the edge set that forms a path such that the first vertex of the path is also the last. A graph containing no cycles of any length is known as an acyclic graph, whereas a graph containing at least one cycle is called cyclic graph. Connected graph: A graph is connected if there is a path from any vertex to every other vertex. Alternatively a graph is connected if there is a path connecting every pair of vertices. An undirected graph that is not connected can be divided into connected components (maximal disjoint connected subgraphs). For example, the disconnected graph below is made of two connected components. Cycle Disconnected Path Connected

More Graph Terminologies Path and cycles in a digraph: must move in the direction specified by the arrows. Connectedness in a digraph: strong and weak. Strongly Connected: If connected as a digraph [There is a path from each vertex to every other vertex] Weakly connected: If the underlying undirected graph is connected [There is at least one vertex that does not have a path to every other vertex] Directed Cycle Strongly Connected Weakly Connected

Strongly Connected Components (SCCs) A disconnected or weakly connected directed graph can be divided into two or more strongly connected components. Two vertices of a directed graph are in the same strongly connected component if and only if they are reachable from each other. A strongly connected component in  a directed graph G = (V, E) is a maximal set of vertices such that for every pair of vertices u and v in the component,  vertices u and v are reachable from each other. Note: If a graph is strongly connected it has only one strongly connected component. Examples: Graph2: Graph1: Graph1 has two strongly connected components Graph3: Graph2 has four strongly connected components Graph3 has three strongly connected components

Further Graph Terminologies Emanating edge: an edge e = (v, w) is said to emanate from v. A(v) denotes the set of all edges emanating from v. Incident edge: an edge e = (v, w) is said to be incident to w. I(w) denote the set of all edges incident to w. Out-degree: number of edges emanating from v: |A(v)| In-degree: number of edges incident to w: |I(w)|

Further Graph Terminologies Subgraph: A graph G = (VG , EG ) is a subgraph of H = (VH, EH) if VG  VH and EG  EH. A spanning subgraph of G is a subgraph that contains all the vertices of G A spanning tree of a connected graph is a spanning subgraph that is a tree

Graph Representations For vertices: an array or a linked list can be used For edges: Adjacency Matrix (Two-dimensional array) Adjacency List (One-dimensional array of linked lists) Linked List (one list only)

Adjacency Matrix Representation Adjacency Matrix uses a 2-D array of dimension |V|x|V| for edges. (For vertices, a 1-D array is used) The presence or absence of an edge, (v, w) is indicated by the entry in row v, column w of the matrix. For an unweighted graph, boolean values could be used. For a weighted graph, the actual weights are used.

Notes on Adjacency Matrix For undirected graph, the adjacency matrix is always symmetric. In a Simple Graph, all diagonal elements are zero (i.e. no edge from a vertex to itself). The space requirement of adjacency matrix is O(n2) - most of it wasted for a graph with few edges. However, entries in the matrix can be accessed directly.

Adjacency List Representation This involves representing the set of vertices adjacent to each vertex as a list. Thus, generating a set of lists. This can be implemented in different ways. Our representation: Vertices as a one dimensional array Edges as an array of linked list (the emanating edges of vertex 1 will be in the list of the first element, and so on, … vertices edges 1 2 3 4   1,2  1,3 Null  2,3  2,4 Null Empty  4,1  4,2  4,3 Null

Simple List Representation Vertices are represented as a 1-D array or a linked list Edges are represented as one linked list Each edge contains the information about its two vertices vertices edges edge(1,2) edge(1,3) edge(2,3) edge(2,4) edge(4,1) edge(4,2) edge(4,3) 1 2 3 4 . .

Comparison between Adjacency Lists and Adjacency Matrices For a Graph G(V, E), let |V| = n and |E| = m – Is there an edge from x to y? • Matrix: O(1) check value at (x, y) • List: O(n) index to x, traverse list to y or end – Edge insertion or deletion Matrix: O(1) List: O(n) - Get successor vertices of a vertex. • Matrix: O(n) scan a row • List: O(n) traverse a linked list – Get predecessor vertices of a vertex. • Matrix: O(n) scan a column • List: O(n + m) traverse all linked lists, which could be as bad as O(n+n2) = O(n2). Visit all edges Matrix: O(n2) List: O(n + m) Space List: O(n + m)

Review Questions Consider the undirected graph GA shown above. List the elements of V and E. Then, for each vertex v in V, do the following: Compute the in-degree of v Compute the out-degree of v List the elements of A(v) List the elements of I(v). Consider the undirected graph GA shown above. Show how the graph is represented using adjacency matrix. Show how the graph is represented using adjacency lists. Repeat Exercises 1 and 2 for the directed graph GB shown above.