1 Graph theory Outline A graph is an abstract data type for storing adjacency relations –We start with definitions: Vertices, edges, degree and sub-graphs.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
 Graph Graph  Types of Graphs Types of Graphs  Data Structures to Store Graphs Data Structures to Store Graphs  Graph Definitions Graph Definitions.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
CSE 326: Data Structures Lecture #19 Graphs I Alon Halevy Spring Quarter 2001.
Connected Components, Directed Graphs, Topological Sort COMP171.
Introduction to Graphs
Introduction to Graphs What is a Graph? Some Example applications of Graphs. Graph Terminologies. Representation of Graphs. –Adjacency Matrix. –Adjacency.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca.
GRAPH Learning Outcomes Students should be able to:
Graph. Data Structures Linear data structures: –Array, linked list, stack, queue Non linear data structures: –Tree, binary tree, graph and digraph.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
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.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
COSC 2007 Data Structures II Chapter 14 Graphs I.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
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.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
GRAPHS. Graph Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected,
Graphs Upon completion you will be able to:
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
1 The tree data structure Outline In this topic, we will cover: –Definition of a tree data structure and its components –Concepts of: Root, internal, and.
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.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
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.
رياضيات متقطعة لعلوم الحاسب MATH 226. Chapter 10.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
1 Single-source shortest path Shortest Path Given a weighted directed graph, one common problem is finding the shortest path between two given vertices.
Outline This topic covers merge sort
Stack.
Topological Sort In this topic, we will discuss: Motivations
Outline The bucket sort makes assumptions about the data being sorted
unweighted path lengths
Poisson distribution.
Open addressing.
All-pairs Shortest Path
All-pairs shortest path
Graphs Chapter 11 Objectives Upon completion you will be able to:
Chapter 9: Graphs Basic Concepts
Outline This topic covers Prim’s algorithm:
Graphs G = (V, E) V are the vertices; E are the edges.
Chapter 9: Graphs Basic Concepts
GRAPHS.
Heaps Chapter 6 Section 6.9.
Presentation transcript:

1 Graph theory Outline A graph is an abstract data type for storing adjacency relations –We start with definitions: Vertices, edges, degree and sub-graphs –We will describe paths in graphs Simple paths and cycles –Definition of connectedness –Weighted graphs –We will then reinterpret these in terms of directed graphs –Directed acyclic graphs

2 Graph theory Outline We will define an Undirected Graph ADT as a collection of vertices V = {v 1, v 2,..., v n } –The number of vertices is denoted by |V| = n –Associated with this is a collection E of unordered pairs {v i, v j } termed edges which connect the vertices There are a number of data structures that can be used to implement abstract undirected graphs –Adjacency matrices –Adjacency lists

3 Graph theory Graphs Consider this collection of vertices V = {v 1, v 2,..., v 9 } where |V| = n

4 Graph theory Undirected graphs Associated with these vertices are |E| = 5 edges E = {{v 1, v 2 }, {v 3, v 5 }, {v 4, v 8 }, {v 4, v 9 }, {v 6, v 9 }} –The pair {v j, v k } indicates that both vertex v j is adjacent to vertex v k and vertex v k is adjacent to vertex v j

5 Graph theory Undirected graphs We will assume in this course that a vertex is never adjacent to itself –For example, {v 1, v 1 } will not define an edge The maximum number of edges in an undirected graph is

6 Graph theory An undirected graph Example: given the |V| = 7 vertices V = {A, B, C, D, E, F, G} and the |E| = 9 edges E = {{A, B}, {A, D}, {A, E}, {B, C}, {B, D}, {B, E}, {C, E}, {C, F}, {D, E}}

7 Graph theory Degree The degree of a vertex is defined as the number of adjacent vertices degree(A) = degree(D) = degree(C) = 3 degree(B) = degree(E) = 4 degree(F) = 1 degree(G) = 0 Those vertices adjacent to a given vertex are its neighbors

8 Graph theory Sub-graphs A sub-graph of a graph is a subset of the vertices and a subset of the edges that connected the subset of vertices in the original graph

9 Graph theory Vertex-induced sub-graphs A vertex-induced sub-graph is a subset of the vertices where the edges are all edges in the original graph that originally connected the selected subset of vertices

10 Graph theory Paths A path in an undirected graph is an ordered sequence of vertices ( v 0, v 1, v 2,..., v k ) where {v j – 1, v j } is an edge for j = 1,..., k –Termed a path from v 0 to v k –The length of this path is k

11 Graph theory Paths A path of length 4: (A, B, E, C, F)

12 Graph theory Paths A path of length 5: (A, B, E, C, B, D)

13 Graph theory Paths A trivial path of length 0: (A)

14 Graph theory Simple paths A simple path has no repetitions other than perhaps the first and last vertices A simple cycle is a simple path of at least two vertices with the first and last vertices equal –Note: these definitions are not universal

15 Graph theory Connectedness Two vertices v i, v j are said to be connected if there exists a path from v i to v j A graph is connected if there exists a path between any two vertices A connected graph An unconnected graph

16 Graph theory Weighted graphs A weight may be associated with each edge in a graph –This could represent distance, energy consumption, cost, etc. –Such a graph is called a weighted graph Pictorially, we will represent weights by numbers next to the edges

17 Graph theory Weighted graphs The length of a path within a weighted graph is the sum of all of the edges which make up the path –The length of the path (A, D, G) in the following graph is = 8.8

18 Graph theory Weighted graphs Different paths may have different weights –Another path is (A, C, F, G) with length = 7.1

19 Graph theory Weighted graphs Problem: find the shortest path between two vertices –Here, the shortest path from A to G is (A, C, F, D, E, G) with length 5.7

20 Graph theory Trees A graph is a tree if it is connected and there is a unique path between any two vertices –Three trees on the same eight vertices Consequences: –The number of edges is |E| = |V| – 1 –The graph is acyclic, that is, it does not contain any cycles –Adding one more edge must create a cycle –Removing any one edge creates two disjoint non-empty sub-graphs

21 Graph theory Trees Any tree can be converted into a rooted tree by: –Choosing any vertex to be the root –Defining its neighboring vertices as its children and then recursively defining: –All neighboring vertices other than that one designated its parent are now defined to be that vertices children Given this tree, here are three rooted trees associated with it

22 Graph theory Forests A forest is any graph that has no cycles Consequences: –The number of edges is |E| < |V| –The number of trees is |V| – |E| –Removing any one edge adds one more tree to the forest Here is a forest with 22 vertices and 18 edges –There are four trees

23 Graph theory Directed graphs In a directed graph, the edges on a graph are associated with a direction –Edges are ordered pairs (v j, v k ) denoting a connection from v j to v k –The edge (v j, v k ) is different from the edge (v k, v j ) Streets are directed graphs: –In most cases, you can go two ways unless it is a one-way street

24 Graph theory Directed graphs Given our graph of nine vertices V = {v 1, v 2, …v 9 } –These six pairs (v j, v k ) are directed edges E = {(v 1, v 2 ), (v 3, v 5 ), (v 5, v 3 ), (v 6, v 9 ), (v 8, v 4 ), (v 9, v 4 )}

25 Graph theory Directed graphs The maximum number of directed edges in a directed graph is

26 Graph theory In and out degrees The degree of a vertex must be modified to consider both cases: –The out-degree of a vertex is the number of vertices which are adjacent to the given vertex –The in-degree of a vertex is the number of vertices which this vertex is adjacent to In this graph: in_degree(v 1 ) = 0out_degree(v 1 ) = 2 in_degree(v 5 ) = 2out_degree(v 5 ) = 3

27 Graph theory Sources and sinks Some definitions: –Vertices with an in-degree of zero are described as sources –Vertices with an out-degree of zero are described as sinks In this graph: –Sources: v 1, v 6, v 7 –Sinks: v 2, v 9

28 Graph theory Paths A path in a directed graph is an ordered sequence of vertices ( v 0, v 1, v 2,..., v k ) where (v j – 1, v j ) is an edge for j = 1,..., k A path of length 5 in this graph is (v 1, v 4, v 5, v 3, v 5, v 2 ) A simple cycle of length 3 is (v 8, v 4, v 5, v 8 )

29 Graph theory Connectedness Two vertices v j, v k are said to be connected if there exists a path from v j to v k –A graph is strongly connected if there exists a directed path between any two vertices –A graph is weakly connected if there exists a path between any two vertices that ignores the direction In this graph: –The sub-graph {v 3, v 4, v 5, v 8 } is strongly connected –The sub-graph {v 1, v 2, v 3, v 4, v 5, v 8 } is weakly connected

30 Graph theory Weighted directed graphs In a weighted directed graphs, each edge is associated with a value Unlike weighted undirected graphs, if both (v j, v k ) and (v j, v k ) are edges, it is not required that they have the same weight

31 Graph theory Directed acyclic graphs A directed acyclic graph is a directed graph which has no cycles –These are commonly referred to as DAGs –They are graphical representations of partial orders on a finite number of elements These two are DAGs: This directed graph is not acyclic:

32 Graph theory Directed acyclic graphs Applications of directed acyclic graphs include: –The parse tree constructed by a compiler –A reference graph that can be garbage collected using simple reference counting –Dependency graphs such as those used in instruction scheduling and makefiles –Dependency graphs between classes formed by inheritance relationships in object-oriented programming languages –Information categorization systems, such as folders in a computer –Directed acyclic word graph data structure to memory-efficiently store a set of strings (words) Reference:

33 Graph theory Representations How do we store the adjacency relations? –Binary-relation list –Adjacency matrix –Adjacency list

34 Graph theory Binary-relation list The most inefficient is a relation list: –A container storing the edges {(1, 2), (1, 4), (3, 5), (4, 2), (4, 5), (5, 2), (5, 3), (5, 8), (6, 9), (7, 9), (8, 4)} –Requires  (|E|) memory –Determining if v j is adjacent to v k is O(|E|) –Finding all neighbors of v j is  (|E|)

35 Graph theory Adjacency matrix Requiring more memory but also faster, an adjacency matrix –The matrix entry (j, k) is set to true if there is an edge (v j, v k ) –Requires  (|V| 2 ) memory –Determining if v j is adjacent to v k is O(1) –Finding all neighbors of v j is  (|V|) TT 2 3T 4TT 5TTT 6T 7T 8T 9

36 Graph theory Adjacency list Most efficient for algorithms is an adjacency list –Each vertex is associated with a list of its neighbors –Requires  (|V| + |E|) memory –On average: Determining if v j is adjacent to v k is Finding all neighbors of v j is 1 → 2 → → 5 4 → 2 → 5 5 → 2 → 3 → 8 6 → 9 7 → 9 8 → 4 9

37 Graph theory The Graph ADT The Graph ADT describes a container storing an adjacency relation –Queries include: The number of vertices The number of edges List the vertices adjacent to a given vertex Are two vertices adjacent? Are two vertices connected? –Modifications include: Inserting or removing an edge Inserting or removing a vertex (and all edges containing that vertex) The run-time of these operations will depend on the representation

38 Graph theory Summary In this topic, we have covered: –Basic graph definitions Vertex, edge, degree, adjacency –Paths, simple paths, and cycles –Connectedness –Weighted graphs –Directed graphs –Directed acyclic graphs We will continue by looking at a number of problems related to graphs

39 Graph theory References Wikipedia, [1]Donald E. Knuth, The Art of Computer Programming, Volume 1: Fundamental Algorithms, 3 rd Ed., Addison Wesley, 1997, §2.2.1, p.238. [2]Cormen, Leiserson, and Rivest, Introduction to Algorithms, McGraw Hill, 1990, §11.1, p.200. [3]Weiss, Data Structures and Algorithm Analysis in C++, 3 rd Ed., Addison Wesley, §3.6, p.94. [4]David H. Laidlaw, Course Notes, These slides are provided for the ECE 250 Algorithms and Data Structures course. The material in it reflects Douglas W. Harder’s best judgment in light of the information available to him at the time of preparation. Any reliance on these course slides by any party for any other purpose are the responsibility of such parties. Douglas W. Harder accepts no responsibility for damages, if any, suffered by any party as a result of decisions made or actions based on these course slides for any other purpose than that for which it was intended.