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.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms CS 477/677
Advertisements

Introduction This chapter explores graphs and their applications in computer science This chapter explores graphs and their applications in computer science.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Graph & BFS.
Connected Components, Directed Graphs, Topological Sort COMP171.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Introduction to Graphs
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
Introduction to Graphs What is a Graph? Some Example applications of Graphs. Graph Terminologies. Representation of Graphs. –Adjacency Matrix. –Adjacency.
Connected Components, Directed Graphs, Topological Sort Lecture 25 COMP171 Fall 2006.
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.
Connected Components, Directed graphs, Topological sort COMP171 Fall 2005.
Chapter 9: Graphs Basic Concepts
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:
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.
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,
Chapter 2 Graph Algorithms.
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
Data Structures Week 9 Introduction to Graphs Consider the following problem. A river with an island and bridges. The problem is to see if there is a way.
 What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree.
1 CS104 : Discrete Structures Chapter V Graph Theory.
Graphs CSE 2011 Winter June Graphs A graph is a pair (V, E), where  V is a set of nodes, called vertices  E is a collection of pairs.
COSC 2007 Data Structures II Chapter 14 Graphs I.
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 황승원 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,
Chapter 9: Graphs.
Introduction to Graph Theory By: Arun Kumar (Asst. Professor) (Asst. Professor)
Chapter 05 Introduction to Graph And Search Algorithms.
1 GRAPH Learning Outcomes Students should be able to: Explain basic terminology of a graph Identify Euler and Hamiltonian cycle Represent graphs using.
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:
رياضيات متقطعة لعلوم الحاسب 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.
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Graphs.
Graph & BFS.
CS 201: Design and Analysis of Algorithms
Data Structures 13th Week
Chapter 9 (Part 2): Graphs
Introduction to Graphs
Basic Concepts Graphs For more notes and topics visit:
Special Graphs By: Sandeep Tuli Astt. Prof. CSE.
Taibah University College of Computer Science & Engineering Course Title: Discrete Mathematics Code: CS 103 Chapter 10 Graphs Slides are adopted from “Discrete.
Introduction to Graphs
Depth-First Search.
CMSC 341 Lecture 21 Graphs (Introduction)
Graph & BFS.
Graphs CSE 2011 Winter November 2018.
Chapter 9: Graphs Basic Concepts
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Graph Operations And Representation
Connected Components, Directed Graphs, Topological Sort
ITEC 2620M Introduction to Data Structures
Graphs G = (V, E) V are the vertices; E are the edges.
Trees-2, Graphs Data Structures with C Chpater-6 Course code: 10CS35
Applied Discrete Mathematics Week 13: Graphs
Based on slides by Y. Peng University of Maryland
Graphs: Definitions How would you represent the following?
Chapter 9: Graphs Basic Concepts
Graphs G = (V,E) V is the vertex set.
GRAPHS.
Introduction to Graphs
Introduction to Graphs
Presentation transcript:

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 hierarchy; for example a family tree or a computer file system. Some data need to have connections between items which do not fit into a hierarchy like this. Graph data structures can be useful in these situations. A graph consists of a number of data items, each of which is called a vertex. Any vertex may be connected to any other, and these connections are called edges.

Graphs Extremely useful tool in modelling problems Consist of: Vertices Edges Vertices can be considered “sites” or locations. Edges represent connections. D E C A F B Vertex Edge

Applications Each vertex represents a city Air flight system Each vertex represents a city Each edge represents a direct flight between two cities A query on direct flights becomes a query on whether an edge exists A query on how to get to a location is “does a path exist from A to B” We can even associate costs to edges (weighted graphs), then ask “what is the cheapest path from A to B”

Another application Wireless communication Can be represented by a weighted complete graph (every two vertices are connected by an edge). Each edge represents the Euclidean distance dij between two stations. Each station uses a certain power i to transmit messages. Given this power i, only a few nodes can be reached (bold edges). A station reachable by i then use its own power to relay the message to other stations not reachable by i. A typical wireless communication problem is: how to broadcast between all stations such that they are all connected and the power consumption is minimized.

To find the least resistance between two nodes. Computer networks: Some other applications: Electronic circuits: To find the least resistance between two nodes. Computer networks: To find the smallest path between 2 computers. Databases: To draw the entity relationship(ER) diagram.

Definition Undirected graph An undirected graph is specified by an ordered pair (V,E), where V is the set of vertices and E is the set of edges {c,f} {a,c} {a,b} {b,d} {c,d} {e,f} {b,e}

Terminology If v1 and v2 are connected, they are said to be adjacent vertices v1 and v2 are endpoints of the edge {v1, v2} If an edge e is connected to v, then v is said to be incident on e. Also, the edge e is said to be incident on v. {v1, v2} = {v2, v1}* *Later, we will talk about “directed graphs”, where edges have direction. This means that {v1,v2} ≠ {v2,v1} . Directed graphs are drawn with arrows (called arcs) between edges. This means {A,B} only, not {B,A} A B

Graph Representation Two popular computer representations of a graph. Both represent the vertex set and the edge set, but in different ways. Adjacency Matrix Use a 2D matrix to represent the graph Adjacency List Use a 1D array of linked lists

Adjacency Matrix Each row and column is indexed by the vertex id. 2D array A[0..n-1, 0..n-1], where n is the number of vertices in the graph Each row and column is indexed by the vertex id. - e,g a=0, b=1, c=2, d=3, e=4 An array entry A [i] [j] is equal to 1 if there is an edge connecting vertices i and j. Otherwise, A [i] [j] is 0. The storage requirement is Θ(n2). Not efficient if the graph has few edges. We can detect in O(1) time whether two vertices are connected.

Adjacency list The adjacency list is an array A[0..n-1] of lists, where n is the number of vertices in the graph. Each array entry is indexed by the vertex id (as with adjacency matrix)‏ The list A[i] stores the ids of the vertices adjacent to i.

Examples 1 9 8 7 6 5 4 3 2 2 4 3 5 1 7 6 9 8

Examples 9 8 7 6 5 4 3 2 1 8 2 4 3 5 1 7 6 9 8 9 7 3 2 8 4 1 5 4 1 3 2 6 3 7 5 6 1 9 2 8 1

Adjacency Lists vs. Matrix More compact than adjacency matrices if graph has few edges Requires more time to find if an edge exists Adjacency Matrix Always require n2 space This can waste a lot of space if the number of edges are sparse Can quickly find if an edge exists

Path between vertices A path is a sequence of vertices (v0, v1, v2,… vk) such that: For 0 ≤ i < k, {vi, vi+1} is an edge For 0 ≤ i < k-1, vi ≠ vi+2 That is, the edge {vi, vi+1} ≠ {vi+1, vi+2} Note: a path is allowed to go through the same vertex or the same edge any number of times! The length of a path is the number of edges on the path

Types of paths A path is simple if and only if it does not contain a vertex more than once. A path is a cycle if and only if v0= vk The beginning and end are the same vertex! A path contains a cycle if some vertex appears twice or more

Examples Are these paths? Any cycles? What is the path’s length? {a,c,f,e} {a,b,d,c,f,e} {a, c, d, b, d, c, f, e} {a,c,d,b,a} {a,c,f,e,b,d,c,a}

A graph is directed if direction is assigned to each edge A graph is directed if direction is assigned to each edge. We call the directed edges arcs. An edge is denoted as an ordered pair (u, v) Recall: for an undirected graph An edge is denoted {u,v}, which actually corresponds to two arcs (u,v) and (v,u) Directed Graph

A path in a directed graph must follow the direction of the arrows. A directed graph is connected if, for any pair of vertices, there is a path between them. Is the above graph connected? D C E A

Representations

Directed Acyclic Graph A directed path is a sequence of vertices (v0, v1, . . . , vk) Such that (vi, vi+1) is an arc A directed cycle is a directed path such that the first and last vertices are the same. A directed graph is acyclic if it does not contain any directed cycles Directed Acyclic Graph

Example 1 2 3 4 5 6 7 8 9 Indeg(2)? Indeg(8)? Outdeg(0)? Num of Edges? 1 2 3 4 5 6 7 8 9 Indeg(2)? Indeg(8)? Outdeg(0)? Num of Edges? Total OutDeg? Total Indeg? Example

Directed graphs are often used to represent order-dependent tasks That is we cannot start a task before another task finishes We can model this task dependent constraint using arcs An arc (i,j) means task j cannot start until task i is finished Clearly, for the system not to hang, the graph must be acyclic. j i Directed Graphs Usage Task j cannot start until task i is finished

Self loops: An edge from an vertex to itself is called self loop. A complete graph is a graph which has maximum number of edges. For undirected graph with n vertices it is n(n-1)/2. For directed graph it is n(n-1). Subgraph of G(V,E): Is a graph G’ =(v’,e’) where v’is subset of V and e’ is subset of E. Other Definitions

Path: A path from vertex vi to vj is a sequence of vertices vi, vi1,vi2……vj such that <vi,vi1><vi1,vi2>…..<vin,vj> edges in the graph. A simple path is a path in which all vertices except possibly the start and end are distinct. A cycle is a simple path in which start and end vertex are same. In an undirected graph two vertices vi and vj are said to be cnnected if there exists a path in G from vi to vj.

Connected graph: An undirected graph is called connected if for every pair of vertices vi and vj there exists a path from vi to vj. A connected component of an undirected graph is the maximal connected subgraph. Tree is an acyclic graph Strongly connected graph:a directed graph is said to be strongly connected if for every pair of vertices vi and vj in V(G) there is a directed path between vi and vj and also from vj and vi.

Strongly connected component: Is a maximal subgraph that is strongly connected. Degree: degree of a vertex is number of edges incident on that vertex. Indegree of v:Number of edges that has v as head in the directed graph. Outdegree of v: Number of edges that has v as tail.