Lecture 5: Graphs & their Representation

Similar documents
CHAPTER 14 GRAPH ALGORITHMS ORD SFO LAX DFW

Chapter 9 Graph Algorithms

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

Chapter 9 Graph Algorithms

CS473-Algorithms I. Lecture 13-A. Graphs. Cevdet Aykanat - Bilkent University Computer Engineering Department

Chapter 9 Graph Algorithms

Logic: The Big Picture. Axiomatizing Arithmetic. Tautologies and Valid Arguments. Graphs and Trees

Graph Theory. ICT Theory Excerpt from various sources by Robert Pergl

CSI 604 Elementary Graph Algorithms

MAT 7003 : Mathematical Foundations. (for Software Engineering) J Paul Gibson, A207.

Directed Graph and Binary Trees

Graphs. Introduction To Graphs: Exercises. Definitions:

EECS 1028 M: Discrete Mathematics for Engineers

CS 4407 Algorithms Lecture 5: Graphs an Introduction

CSE 101, Winter Discussion Section Week 1. January 8 - January 15

Graphs Definitions. Gunnar Gotshalks. GraphDefinitions 1

IS 709/809: Computational Methods in IS Research. Graph Algorithms: Introduction

Graphs ADTs and Implementations

BACKGROUND: A BRIEF INTRODUCTION TO GRAPH THEORY

Graph Theory CS/Math231 Discrete Mathematics Spring2015

Graphs & Digraphs Tuesday, November 06, 2007

Lecture 22 Tuesday, April 10

Review: Graph Theory and Representation

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

Foundations of Discrete Mathematics

Graph and Digraph Glossary

Discrete mathematics II. - Graphs

CS200: Graphs. Prichard Ch. 14 Rosen Ch. 10. CS200 - Graphs 1

Chapter 2 Graphs. 2.1 Definition of Graphs

CMSC 380. Graph Terminology and Representation

March 20/2003 Jayakanth Srinivasan,

Graphs. Outline. Definitions Graph implementation as data structure Visiting algorithms C implementations

Graphs V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)}

An Introduction to Graph Theory

(5.2) 151 Math Exercises. Graph Terminology and Special Types of Graphs. Malek Zein AL-Abidin

Graphs Introduction and Depth first algorithm

Introduction to Graphs

Directed acyclic graphs

Introduction to Graph Theory

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

CHAPTER 2. Graphs. 1. Introduction to Graphs and Graph Isomorphism


GRAPH THEORY - FUNDAMENTALS

Graph definitions. There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs. An undirected graph

UNDIRECTED GRAPH: a set of vertices and a set of undirected edges each of which is associated with a set of one or two of these vertices.

Lecture 26: Graphs: Traversal (Part 1)

Graphs. Data Structures and Algorithms CSE 373 SU 18 BEN JONES 1

Chapter 11: Graphs and Trees. March 23, 2008

Graph. Vertex. edge. Directed Graph. Undirected Graph

COT 6405 Introduction to Theory of Algorithms

Artificial Intelligence

Introduction III. Graphs. Motivations I. Introduction IV

Graphs and Genetics. Outline. Computational Biology IST. Ana Teresa Freitas 2015/2016. Slides source: AED (MEEC/IST); Jones and Pevzner (book)

ROOT A node which doesn t have a parent. In the above tree. The Root is A. LEAF A node which doesn t have children is called leaf or Terminal node.

Graphs: basic concepts and algorithms

DS UNIT 4. Matoshri College of Engineering and Research Center Nasik Department of Computer Engineering Discrete Structutre UNIT - IV

Varying Applications (examples)

CS 270 Algorithms. Oliver Kullmann. Analysing BFS. Depth-first search. Analysing DFS. Dags and topological sorting.

Undirected Graphs. Hwansoo Han

CS 206 Introduction to Computer Science II

1 Digraphs. Definition 1

Graphs and trees come up everywhere. We can view the internet as a graph (in many ways) Web search views web pages as a graph

CPCS Discrete Structures 1

Graphs. A graph is a data structure consisting of nodes (or vertices) and edges. An edge is a connection between two nodes

Discrete Structures CISC 2315 FALL Graphs & Trees

Algorithm Design (8) Graph Algorithms 1/2

2pt 0em. Computer Science & Engineering 423/823 Design and Analysis of Algorithms. Lecture 04 Minimum-Weight Spanning Trees (Chapter 23)

Chapter 3 Trees. Theorem A graph T is a tree if, and only if, every two distinct vertices of T are joined by a unique path.

Breadth First Search. cse2011 section 13.3 of textbook

Information Science 2

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

Section 8.2 Graph Terminology. Undirected Graphs. Definition: Two vertices u, v in V are adjacent or neighbors if there is an edge e between u and v.

Definition of Graphs and Trees. Representation of Trees.

Inf 2B: Graphs II - Applications of DFS

Greedy Algorithms. At each step in the algorithm, one of several choices can be made.

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies:

Week 5. 1 Analysing BFS. 2 Depth-first search. 3 Analysing DFS. 4 Dags and topological sorting. 5 Detecting cycles. CS 270 Algorithms.

Graph Algorithms: Chapters Part 1: Introductory graph concepts

Some Graph Theory for Network Analysis. CS 249B: Science of Networks Week 01: Thursday, 01/31/08 Daniel Bilar Wellesley College Spring 2008

CS 270 Algorithms. Oliver Kullmann. Breadth-first search. Analysing BFS. Depth-first. search. Analysing DFS. Dags and topological sorting.

UNIT III TREES. A tree is a non-linear data structure that is used to represents hierarchical relationships between individual data items.

Outline. Introduction. Representations of Graphs Graph Traversals. Applications. Definitions and Basic Terminologies

DEFINITION OF GRAPH GRAPH THEORY GRAPHS ACCORDING TO THEIR VERTICES AND EDGES EXAMPLE GRAPHS ACCORDING TO THEIR VERTICES AND EDGES

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

Fundamentals of Data Structure

MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)

Graph Theory. Probabilistic Graphical Models. L. Enrique Sucar, INAOE. Definitions. Types of Graphs. Trajectories and Circuits.

CS 441 Discrete Mathematics for CS Lecture 26. Graphs. CS 441 Discrete mathematics for CS. Final exam

6. Lecture notes on matroid intersection

Graph Search. CS/ECE 374: Algorithms & Models of Computation, Fall Lecture 15. October 18, 2018

Lecture 9 Graph Traversal

Graph Theory. Part of Texas Counties.

CSE 2331/5331. Topic 9: Basic Graph Alg. Representations Basic traversal algorithms Topological sort CSE 2331/5331

7.3 Spanning trees Spanning trees [ ] 61

Algorithm Design and Analysis

Graph Algorithms. Definition

Copyright 2000, Kevin Wayne 1

CS2 Algorithms and Data Structures Note 10. Depth-First Search and Topological Sorting

Figure 1: A directed graph.

Transcription:

Lecture 5: Graphs & their Representation Why Do We Need Graphs Graph Algorithms: Many problems can be formulated as problems on graphs and can be solved with graph algorithms. To learn those graph algorithms, we need basic knowledge of graphs. Graphs: A graph describes some relation among a set of objects. Examples of Applications: Communication and transportation networks, logic circuits, computer aided designs, etc. 1

Graphs Continued There are various types of graphs. Two basic types are undirected graphs (usually just called graphs) directed graphs (sometimes called digraphs) 2

Undirected graphs Definition: An (undirected) graph G = (V, E) consists of two sets (components), a set V of vertices (nodes) and a set E of edges. E is a set of 2-subsets of V, that is, each edge is of the form {v, w}. Example of an (undirected) graph: V = {1, 2, 3, 4, 5, 6}. E = {{1, 2}, {2, 5}, {1, 5}, {3, 6}} Remark: In this course, unless otherwise stated, all graphs dealt with do not have self-loops (that is, v w) and do not have parallel edges (that is, all edges are distinct). 3

Graphs Continued Definition: A directed graph G = (V, E) consists of two sets (components), a set V of vertices (nodes) and a set E of edges. E is a subset of V V, that is, each edge is of the form (v, w), a directed edge from v to w. Example of a digraph: V = {1, 2, 3, 4, 5, 6}. E = {(1, 2), (2, 4), (4, 1), (2, 5), (4, 5), (5, 4), (6, 3)}. Remark:Again, unless otherwise stated, all graphs dealt with do not have self-loops and do not have parallel edges. 4

Representation We will consider various (equivalent) representations of graphs: Set description (as in definition). Mathematical Pictorial representation. For explanations Adjacency matrix representation. In computer Adjacency list representation. In computer 5

Pictorial Representation Example, (undirected) graph: V = {1, 2, 3, 4, 5, 6}. E = {{1, 2}, {2, 5}, {1, 5}, {3, 6}} Example, digraph: V = {1, 2, 3, 4, 5, 6}. E = {(1, 2), (2, 4), (4, 1), (2, 5), (4, 5), (5, 4), (6, 3)}. 6

Matrix Representation Adjacency Matrix of Digraphs: Let G = (V, E) be a directed graph, where V is indexed by {1, 2,..., n}. Its n n adjacency matrix of G is defined by A[v, w] = { 1 if (v, w) E, 0 otherwise Adjacency Matrix of Graphs: Let G = (V, E) be a graph, where V is indexed by {1, 2,..., n}. The n n adjacency matrix of G is defined by A[v, w] = { 1 if {v, w} E, 0 otherwise Remark: The adjacency matrix of a digraph might not be symmetric, while that of undirected graphs must be symmetric. 7

Matrix Representation Continued Example, undirected graph: The adjacency matrix of is 0 1 0 0 1 0 1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0. 8

Matrix Representation Continued Example, directed graph: The adjacency matrix of is 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0. 9

Adjacency List An Adjacency List is an array Adj[1...n] of pointers, where Adj[u] points to a linked list containing the vertices u such that {u, v} (undirected) or (u, v) (directed) is an edge. Example (Attention: A loop at vertex 2). 1 2 2 2 4 5 3 4 1 5 5 4 6 3 10

The Best Representation Which is better to use, adjacency matrics or adjacency lists? The answer depends upon what operations your algorithm needs to perfom Operation Adjacency List Adjacency Matrix Initialization (space needed) Θ( E + V ) Θ( V 2 ) Scan all Edges once Θ( E + V ) Θ( V 2 ) Check if (u, v) E Θ( V ) O(1) 11

Weighted Graphs Definition: A weighted graph (digraph) is a graph in which there is a number associated with each edge called the weight of the edge. This weight could represent many things, e.g., the length of a road connecting u to v, the cost of shipping an item from u to v, the capacity of a pipeline connecting u to v, etc.. Examples: 1 2 3 7.8 11 7.6 8.1 9 12 8.8 5 11 6 10.5 12

For weighted graphs (and digraphs), we can store the weights in a matrix similar to the adjacency matrix. For example, for an edge {v, w} (or (v, w)), define the weight of the edge. A[v, w] = W (v, w), Otherwise we define A[v, w] to be, which is larger than any number. The adjacency list data structure can similarly be augmented to store weights. 13

1 2 3 7.8 11 7.6 8.1 9 12 8.8 5 11 6 10.5 7.8 11 8.1 7.6 9 8.8 12. 5 11 5 6 10.5 11 6 10.5. 14

Incidence Incident from & to: In a directed graph G = (V, E), an edge (u, v) is incident from or leaves u, and is incident to or enters v. Example: In the digraph the edge (1, 2) is incident from 1 and is incident to 2. 15

Incident Incident on: In an undirected graph G = (V, E), an edge {u, v} is said to be incident on both u and v. Example: In the undirected graph the edge {3, 6} is incident on both 3 and 6. 16

Adjacent A vertex v is adjacent to u if there is an edge from u to v. For example, in the digraph below, 2 is adjacent to 1. In the undirected graph below, 1 and 2 are adjacent vertices. 17

Degree of Vertices and Graphs Degrees: The degree of a vertex in an undirected graph is the number of edges incident on it, with a loop being counted twice. The (total) degree is the sum of the degrees of all vertices. Example: For the undirected graph we have deg(1) = deg(2) = deg(5) = 2, deg(3) = deg(6) = 1, deg(4) = 0. So deg(g) = 6 i=1 deg(i) = 8. Degree Formula: For a graph G = (V, E), deg(g) = deg(v) = 2 E. v V 18

Degree of Vertices and Graphs Continued In a digraph, the outdegree of a vertex is the number of edges leaving it, and the indegree of a vertex is the number of edges entering it. The degree of a vertex in a directed graph is its indegree plus outdegree. Example: For the directed graph we have indegree(1) = outdegree(1) = 1, indegree(2) = 1, outdegree(2) = 2, indegree(5) = 2, outdegree(5) = 1. Degree Formula: For a digraph G = (V, E), indegree(v) = outdegree(v) = E. v V v V 19

Paths Path: A path in a directed graph is a sequence of vertices such that for i = 1, 2,.., k. v 0, v 1,, v k (v i 1, v i ) E The length of the path is the number of edges, k. We say that w is reachable from u if there is a path from u to w. A path is simple if all vertices are distinct. Example: In the digraph 2, 4, 5, 4 is a path of length 3. It is not simple. 20

Cycles Cycle: In a directed graph, a cycle is a path v 0, v 1,, v k in which v 0 = v k. For example, 1, 2, 5, 4, 1, 5, 4, 5, 1, 2, 4, are cycles in the graph Paths and Cycles in Undirected Graphs are similarly defined. A cycle v 0, v 1,, v k is simple if the path v 0, v 1,, v k 1 is simple. 21

Special Graphs Connected Graph: An undirected graph is connected if every pair of vertices is connected by a path. a d 1 4 c 3 b e 2 5 connected unconnected Strongly Connected Digraph: A directed graph is strongly connected if for any pair of vertices one can be reachable from the other. a d 1 4 c b e strongly connected 3 2 5 not strongly connected 22

Special Graphs Continued Acyclic Graphs: A graph with no cycle is acyclic. DAG = directed acyclic graph. Forest: An acyclic, undirected graph is a forest. For example, the undirected graph below is not a forest, as it has cycles. Tree: An acyclic, connected undirected graph is a tree. Comments on Forests and Trees: Any forest must be composed of a number of trees. 23

Special Graphs Trees Trees: A tree is a connected, acyclic, undirected graph. For example, Graph (a) is a tree, while (b) is a forest: (a) A tree. (b) A forest Properties of Trees: Let G = (V, E) be an undirected graph. The following statements are equivalent: (1) G is a tree. (2) Any two vertices in G are connected by a unique simple path. (3) G is connected, and E = V 1. (4) G is acyclic, and E = V 1. 24

Rooted Trees and Ordered Trees Rooted Tree: A rooted tree is a tree in which one of the vertices is distinguished from the others. The distinguished vertex is called the root of the tree. Root Depth 0 height = 3 Depth 1 Depth 2 Rooted tree Depth 3 Some Basic Notions: Height, depth (level), nodes (internal vertices), leaves, children. Ordered Tree: An ordered tree is a tree in which the children of each node are ordered. 25

Binary Trees Binary Tree: In a binary tree every node has at most two children. We distinguish between left children and right children. 26