Graphs. 2 Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.

Slides:



Advertisements
Similar presentations
Review Binary Search Trees Operations on Binary Search Tree
Advertisements

Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
CSE 373, Copyright S. Tanimoto, 2001 Graphs Graphs 2 Incidence and Adjacency Representing a graph with an adjacency matrix, an incidence matrix,
PSU CS Algorithms Analysis and Design Graphs.
CSE 373 Graphs 1: Concepts, Depth/Breadth-First Search
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
CSE 373: Data Structures and Algorithms Lecture 19: Graphs III 1.
Fundamentals of Python: From First Programs Through Data Structures
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)
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
Graphs.
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.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs.
17-Jun-15 Searching in BeeperHunt (Mostly minor variations on old slides)
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.
Graphs. Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
Introduction to Graphs
Introduction to Graphs What is a Graph? Some Example applications of Graphs. Graph Terminologies. Representation of Graphs. –Adjacency Matrix. –Adjacency.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
Using Search in Problem Solving
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
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.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
14 Graph ADTs  Graph concepts.  Graph applications.  A graph ADT: requirements, contract.  Implementations of graphs: edge-set, adjacency-set, adjacency-matrix.
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
Computer Science 112 Fundamentals of Programming II Introduction to Graphs.
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.
State-Space Searches. 2 State spaces A state space consists of A (possibly infinite) set of states The start state represents the initial problem Each.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs and Graph Algorithms Fundamentals, Terminology, Traversal, Algorithms SoftUni Team Technical Trainers Software University
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
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++
Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
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.
Graph Terminologies Department of CE/IT M.S.P.V.L.Polytechnic College,
Graphs Upon completion you will be able to:
Copyright © Curt Hill Graphs Definitions and Implementations.
Chapter 9: Graphs.
Graphs and Paths : Chapter 15 Saurav Karmakar
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.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
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.
Graphs. What is a graph? In simple words, A graph is a set of vertices and edges which connect them. A node (or vertex) is a discrete position in the.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Graphs.
Tree Searching.
Graphs.
Graphs.
Graphs.
Graphs.
Graphs.
Heaps Chapter 6 Section 6.9.
Presentation transcript:

Graphs

2 Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge Bristol Southhampton Dover An undirected graph start fill pan with water take egg from fridge break egg into pan boil water add salt to water A directed graph

3 Graph terminology I A graph is a collection of nodes (or vertices, singular is vertex) and edges (or arcs) Each node contains an element Each edge connects two nodes together (or possibly the same node to itself) and may contain an edge attribute A directed graph is one in which the edges have a direction An undirected graph is one in which the edges do not have a direction Note: Whether a graph is directed or undirected is a logical distinction—it describes how we think about the graph Depending on the implementation, we may or may not be able to follow a directed edge in the “backwards” direction

4 Graph terminology II The size of a graph is the number of nodes in it The empty graph has size zero (no nodes) If two nodes are connected by an edge, they are neighbors (and the nodes are adjacent to each other) The degree of a node is the number of edges it has For directed graphs, If a directed edge goes from node S to node D, we call S the source and D the destination of the edge The edge is an out-edge of S and an in-edge of D S is a predecessor of D, and D is a successor of S The in-degree of a node is the number of in-edges it has The out-degree of a node is the number of out-edges it has

5 Graph terminology III A path is a list of edges such that each node (but the last) is the predecessor of the next node in the list A cycle is a path whose first and last nodes are the same Example: (London, Bristol, Birmingham, London, Dover) is a path Example: (London, Bristol, Birmingham, London) is a cycle A cyclic graph contains at least one cycle An acyclic graph does not contain any cycles Birmingham Rugby London Cambridge Bristol Southhampton Dover

6 Graph terminology IV An undirected graph is connected if there is a path from every node to every other node A directed graph is strongly connected if there is a path from every node to every other node A directed graph is weakly connected if the underlying undirected graph is connected Node X is reachable from node Y if there is a path from Y to X A subset of the nodes of the graph is a connected component (or just a component) if there is a path from every node in the subset to every other node in the subset

7 Adjacency-matrix representation I One simple way of representing a graph is the adjacency matrix A 2-D array has a mark at [i][j] if there is an edge from node i to node j The adjacency matrix is symmetric about the main diagonal A B G E F D C ABCDEFGABCDEFG A B C D E F G

8 Adjacency-matrix representation II An adjacency matrix can equally well be used for digraphs (directed graphs) A 2-D array has a mark at [i][j] if there is an edge from node i to node j Again, this is only suitable for small graphs! A B G E F D C ABCDEFGABCDEFG A B C D E F G

9 Edge-set representation I An edge-set representation uses a set of nodes and a set of edges The sets might be represented by, say, linked lists The set links are stored in the nodes and edges themselves The only other information in a node is its element (that is, its value)—it does not hold information about its edges The only other information in an edge is its source and destination (and attribute, if any) If the graph is undirected, we keep links to both nodes, but don’t distinguish between source and destination

10 Edge-set representation II Here we have a set of nodes, and each node contains only its element (not shown) A B G E F D C p q r s t u v w Each edge contains references to its source and its destination (and its attribute, if any) edgeSet = { p: (A, E), q: (B, C), r: (E, B), s: (D, D), t: (D, A), u: (E, G), v: (F, E), w: (G, D) } nodeSet = {A, B, C, D, E, F, G}

11 Adjacency-set representation I An adjacency-set representation uses a set of nodes Each node contains a reference to the set of its edges For a directed graph, a node might only know about (have references to) its out-edges Thus, there is not one single edge set, but rather a separate edge set for each node Each edge would contain its attribute (if any) and its destination (and possibly its source)

12 Adjacency-set representation II Here we have a set of nodes, and each node refers to a set of edges A B G E F D C p q r s t u v w A { p } B { q } C { } D { s, t } E { r, u } F { v } G { w } Each edge contains references to its source and its destination (and its attribute, if any) p: (A, E) q: (B, C) r: (E, B) s: (D, D) t: (D, A) u: (E, G) v: (F, E) w: (G, D)

13 Adjacency-set representation III If the edges have no associated attribute, there is no need for a separate Edge class Instead, each node can refer to a set of its neighbors In this representation, the edges would be implicit in the connections between nodes, not a separate data structure For an undirected graph, the node would have references to all the nodes adjacent to it For a directed graph, the node might have: references to all the nodes adjacent to it, or references to only those adjacent nodes connected by an out- edge from this node

14 Adjacency-set representation IV Here we have a set of nodes, and each node refers to a set of other (pointed to) nodes The edges are implicit A B G E F D C A { E } B { C } C { } D { D, A } E { B, G } F { E } G { D }

15 Searching a graph With certain modifications, any tree search technique can be applied to a graph This includes depth-first, breadth-first, depth-first iterative deepening, and other types of searches The difference is that a graph may have cycles We don’t want to search around and around in a cycle To avoid getting caught in a cycle, we must keep track of which nodes we have already explored There are two basic techniques for this: Keep a set of already explored nodes, or Mark the node itself as having been explored Marking nodes is not always possible (may not be allowed)

16 Example: Depth-first search Here is how to do DFS on a tree: Put the root node on a stack; while (stack is not empty) { remove a node from the stack; if (node is a goal node) return success; put all children of the node onto the stack; } return failure; Here is how to do DFS on a graph: Put the starting node on a stack; while (stack is not empty) { remove a node from the stack; if (node has already been visited) continue; if (node is a goal node) return success; put all adjacent nodes of the node onto the stack; } return failure;

17 Finding connected components A depth-first search can be used to find connected components of a graph A connected component is a set of nodes; therefore, A set of connected components is a set of sets of nodes To find the connected components of a graph: while there is a node not assigned to a component { put that node in a new component do a DFS from the node, and put every node reached into the same component }

18 Graph applications Graphs can be used for: Finding a route to drive from one city to another Finding connecting flights from one city to another Determining least-cost highway connections Designing optimal connections on a computer chip Implementing automata Implementing compilers Doing garbage collection Representing family histories Doing similarity testing (e.g. for a dating service) Pert charts Playing games

19 Summary A graph may be directed or undirected The edges (=arcs) may have weights or contain other data, or they may be just connectors Similarly, the nodes (=vertices) may or may not contain data There are various ways to represent graphs The “best” representation depends on the problem to be solved You need to consider what kind of access needs to be quick or easy Many tree algorithms can be modified for graphs Basically, this means some way to recognize cycles

20 A graph puzzle Suppose you have a directed graph with the above shape You don’t know how many nodes are in the leader You don’t know how many nodes are in the loop You don’t know how many nodes there are total You aren’t allowed to mark nodes Devise an O(n) algorithm ( n being the total number of nodes) to decide when you must already be in the loop This is not asking to find the first node in the loop You can only use a fixed (constant) amount of extra memory nodes in leadernodes in loop start here

21 The End