ALG0183 Algorithms & Data Structures Lecture 18 The basics of graphs. 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Introduction This chapter explores graphs and their applications in computer science This chapter explores graphs and their applications in computer science.
 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.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R5. Graphs.
Graph & BFS.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Chapter 9 Graph algorithms. Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
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.
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.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Chapter 9: Graphs Basic Concepts
Graph Operations And Representation. Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
GRAPHS Education is what remains after one has forgotten what one has learned in school. Albert Einstein Albert Einstein Smitha N Pai.
Graph. Data Structures Linear data structures: –Array, linked list, stack, queue Non linear data structures: –Tree, binary tree, graph and digraph.
Graphs CS /02/05 Graphs Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Definition.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Lecture 13 Graphs. Introduction to Graphs Examples of Graphs – Airline Route Map What is the fastest way to get from Pittsburgh to St Louis? What is the.
Computer Science 112 Fundamentals of Programming II Introduction to Graphs.
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.
Graphs. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes to each other The set of.
Introduction to Graphs. Introduction Graphs are a generalization of trees –Nodes or verticies –Edges or arcs Two kinds of graphs –Directed –Undirected.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
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.
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.
Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, Based on recitation.
Data Structures & Algorithms Graphs
COSC 2007 Data Structures II Chapter 14 Graphs I.
Introduction to Graph Theory
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.
Mudasser Naseer 1 1/9/2016 CS 201: Design and Analysis of Algorithms Lecture # 17 Elementary Graph Algorithms (CH # 22)
Homework #5 Due: October 31, 2000 Christine Kang Graph Concepts and Algorithms.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
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,
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
Graphs Upon completion you will be able to:
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.
Graph Representations And Traversals. Graphs Graph : – Set of Vertices (Nodes) – Set of Edges connecting vertices (u, v) : edge connecting Origin: u Destination:
Graph Theory Graph Theory - History Leonhard Euler's paper on “Seven Bridges of Königsberg”, published in 1736.
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:
Basic Concepts Graphs For more notes and topics visit:
CMSC 341 Lecture 21 Graphs (Introduction)
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:
CS223 Advanced Data Structures and Algorithms
Chapter 9: Graphs Basic Concepts
Graph Operations And Representation
CSCI2100 Data Structures Tutorial
Graphs G = (V, E) V are the vertices; E are the edges.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Chapter 9: Graphs Basic Concepts
Graphs G = (V,E) V is the vertex set.
Chapter 9 Graph algorithms
Presentation transcript:

ALG0183 Algorithms & Data Structures Lecture 18 The basics of graphs. 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks

8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 2 node or vertex or point of the graph edge or arc or line of the graph node/hnútur arc/ör

n = 1 n = 2 n = 3 A complete,undirected graph has all possible edges. n = 4 8/25/20093 ALG0183 Algorithms & Data Structures by Dr Andy Brooks An n-vertex, complete, undirected graph has n(n-1)/2 edges. (excluding self-loops)

London Tube Map 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 4... graphs are everywhere in the real-world. A vertex represents a tube station and might store the station name. An edge represents a railway route between tube stations.

Áfangastaðir Flugfélags Íslands 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 5... graphs are everywhere in the real-world. A vertex represents an airport and might store the airport code. An edge represents a flight route between two airports and might store the flight time. But we might need two edges for each flight route because flying with and against the wind should be costed differently and/or because the airport taxes might be different. destinations/ áfangastaðir

8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 6 Graph definition Definition: A set of items connected by edges. Each item is called a vertex or node. Formally, a graph is a set of vertices and a binary relation between vertices, adjacency.edgesvertexnodesetbinary relation Formal Definition: A graph G can be defined as a pair (V,E), where V is a set of vertices, and E is a set of edges between the vertices E ⊆ {(u,v) | u, v ∈ V}. If the graph is undirected, the adjacency relation defined by the edges is symmetric, or E ⊆ {{u,v} | u, v ∈ V} (sets of vertices rather than ordered pairs). If the graph does not allow self-loops, adjacency is irreflexive.symmetricself-loops irreflexive Specialization (... is a kind of me.) directed graph, undirected graph, acyclic graph, directed acyclic graph, planar graph, biconnected graph, connected graph, complete graph, dense graph, sparse graph, hypergraph, multigraph, labeled graph, weighted graph, tree. directed graphundirected graphacyclic graphdirected acyclic graph planar graphbiconnected graphconnected graphcomplete graph dense graphsparse graphhypergraphmultigraphlabeled graph weighted graphtree

8/25/20097 A graph G = (V,E) G = (V,E) – V is the vertex set, E is the edge set. – Vertices are also called nodes and points. – Edges are also called arcs and lines. Each edge connects two different vertices. An undirected edge has no orientation (u,v). – edge (u,v) is the same as edge (v,u) A directed edge has an orientation (u,v). – edge (u,v) is not the same as edge (v,u) Undirected graph => no oriented edge. Directed graph (“digraph”) => every edge has an orientation. uv uv ALG0183 Algorithms & Data Structures by Dr Andy Brooks one-way street one-way street/einstefnugata

8/25/20098 Some Graphs incomplete & directed Number of edges in (a) <= n(n-1)/2 Number of edges in (c) <= n(n-1) Unconnected. incomplete & undirected incomplete & undirected & unconnected Sahni, © McGraw-Hill ALG0183 Algorithms & Data Structures by Dr Andy Brooks

8/25/20099 Some Spanning Trees Connected, undirected subgraphs (no cycles) that include all vertices of the original graph. n vertices, n-1 edges Sahni, © McGraw-Hill ALG0183 Algorithms & Data Structures by Dr Andy Brooks

8/25/ Street map application e.g. guiding taxi drivers If we associated lengths with each edge, we would have a weighted digraph and we could try to find the shortest way of getting from intersection i to intersection j. Sahni, © McGraw-Hill ALG0183 Algorithms & Data Structures by Dr Andy Brooks

8/25/ Weighted graph example The weights might represent the cost of upgrading roads or laying optical cables between cities. Sahni, © McGraw-Hill ALG0183 Algorithms & Data Structures by Dr Andy Brooks Question: Is 110 the minimum cost spanning tree? We need to use Kruskal's algorithm.

Edges incident on a vertex – a, d, and b are incident on V in (1) and (2) Adjacent vertices: – A vertex u is adjacent (or next) to v if there is an edge from v to u. – In (1), U is adjacent to V, but not vice versa. – In (2), U and V are adjacent to each other Degree of a vertex – X has degree 2 in (1) – X has degree 5 in (2) – X has in-degree 2 in (1) – X has out-degree 0 in (1) Parallel edges – h and i are parallel edges Self-loop – j is a self-loop 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 12 based on a slide by Tangming Yuan

Path – a sequence of vertices connected by edges – each successive vertex is adjacent to its predecessor Simple path – path such that all its vertices in the path are distinct Examples – P1=(V, X, Z) is a simple path – P2=(U, W, X, Y, W, V) is a path that is not simple 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 13 based on a slide by Tangming Yuan

Cycle – a path in which the first and final vertices are the same Simple cycle – cycle such that all its vertices and edges are distinct except the first and final vertices Examples – C1 = (V, X, Y, W, U, V) is a simple cycle – C2 = (U, W, X, Y, W, V, U) is a cycle that is not simple 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 14 based on a slide by Tangming Yuan A directed graph having no cycles is called a directed acyclic graph (DAG).

8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 15 adjacency-matrix representation Definition: A representation of a directed graph with n vertices using an n × n matrix, where the entry at (i,j) is 1 if there is an edge from vertex i to vertex j; otherwise the entry is 0. A weighted graph may be represented using the weight as the entry. An undirected graph may be represented using the same entry in both (i,j) and (j,i) or using an upper triangular matrix. (Definition: A matrix that is only defined at (i,j) when i ≤ j.)directed graphverticesmatrixedgeweighted graphundirected graphupper triangular matrixmatrix example by Dr Srinivasan Ramaswamy

8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 16 adjacency-list representation Definition: A representation of a directed graph with n vertices using an array of n lists of vertices. List i contains vertex j if there is an edge from vertex i to vertex j. A weighted graph may be represented with a list of vertex/weight pairs. An undirected graph may be represented by having vertex j in the list for vertex i and vertex i in the list for vertex j.directed graphverticesarraylistsedgeweighted graphundirected graph The adjacency-list representation is more compact for a sparse matrix. (Definition: A matrix that has relatively few non-zero (or "interesting") entries. It may be represented in much less than n × m space.)sparse matrix “In most applications, however, a sparse graph is the norm.” Weiss e.g. an airport does not have a direct flight route to every other airport...

8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 17 Note: Suppose we have a directed graph with four vertices. Here are adjacency-matrix and adjacency-list representations. The arrow (->) means a link in a list > 1 -> 2 -> 3 -> 4 2 -> 1 3 -> 2 -> 4 4 -> 2 -> 3 Big-Oh(space) is|V| 2 When all the edges are present |E| = |V| 2. Big-Oh(space) is |V|+|E| -> |E| Algorithms developed for sparse graphs typically use adjacency-list representations. A graph G = (V,E) list order “unimportant”

Figure 14.1 A directed graph Weiss ©Addison Wesley 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 18 The shortest path between V 0 and V 5 has two edges with a cost of 1+8=9. The weighted shortest path between V 0 and V 5 has three edges and a cost of 1+4+1=6.