Definition of Graphs A graph is a finite set of nodes with edges between nodes Formally, a graph G is a structure (V,E) consisting of a finite set V called.

Slides:



Advertisements
Similar presentations
CS 1031 Graphs Definition of Graphs and Related Concepts Representation of Graphs The Graph Class Graph Traversal Graph Applications.
Advertisements

Graphs COP Graphs  Train Lines Gainesville OcalaDeltona Daytona Melbourne Lakeland Tampa Orlando.
Review Binary Search Trees Operations on Binary Search Tree
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Topological Sort.
Data Structures Using C++
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Data Structures Using C++
Data Structures Using Java1 Chapter 11 Graphs. Data Structures Using Java2 Chapter Objectives Learn about graphs Become familiar with the basic terminology.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Introduction to Graphs
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
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.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
Data Structures Using C++ 2E
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
CS 1031 Trees A Quick Introduction to Graphs Definition of Trees Rooted Trees Binary Trees Binary Search Trees.
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.
ITEC 2620A Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: 2620a.htm Office: TEL 3049.
10 Copyright © William C. Cheng Data Structures - CSCI 102 Graph Terminology A graph consists of a set of Vertices and a set of Edges C A B D a c b d e.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
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.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Chapter 9: Graphs.
Chapter 05 Introduction to Graph And Search Algorithms.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
1 Definition of Graphs A graph is a finite set of nodes with edges between nodes Formally, a graph G is a structure (V,E) consisting of –a finite set V.
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
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 A New Data Structure
Graphs Chapter 20.
Graphs Definition of Graphs and Related Concepts
Elementary Graph Algorithms
Unit 10 Graphs (1) King Fahd University of Petroleum & Minerals
Graphs Representation, BFS, DFS
Data Structures 13th Week
Introduction to Graphs
Definition of Graphs A graph is a finite set of nodes with edges between nodes Formally, a graph G is a structure (V,E) consisting of a finite set V called.
Csc 2720 Instructor: Zhuojun Duan
Introduction to Graphs
Introduction to Graphs
Depth-First Search.
CS202 - Fundamental Structures of Computer Science II
Graph.
Graphs Representation, BFS, DFS
Graphs Chapter 13.
Graphs Definition of Graphs and Related Concepts
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:
Graphs.
Graphs.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Chapter 11 Graphs.
ITEC 2620M Introduction to Data Structures
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Graphs Chapter 7 Visit for more Learning Resources.
Data Structures and Algorithm Analysis Graph Algorithms
Graphs.
Graphs.
Graph Algorithms DS.GR.1 Chapter 9 Overview Representation
Graphs.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Trees A Quick Introduction to Graphs Definition of Trees Rooted Trees
Graphs.
Introduction to Graphs
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

Definition of Graphs A graph is a finite set of nodes with edges between nodes Formally, a graph G is a structure (V,E) consisting of a finite set V called the set of nodes, and a set E that is a subset of VxV. That is, E is a set of pairs of the form (x,y) where x and y are nodes in V

Motivation What are some characteristics of a binary tree? They have a rigid structure of each node having a single node that points to it (or none, in the case of the root). Sometimes life isn’t so structured. For example: I need to fly to Tokyo. I want to find the cheapest way to fly there. How could I think about the data? For example: I have a set of candy bars in this bag. I want to distribute so everybody gets a kind they like. How do I represent the data? Human graph. If you could pick one person who you would go to for help with an assignment, who would it be? Using your arm as a link, touch that person on the shoulder. What if you could choose two people? For example: I want to travel every bike path in Logan (assuming they had any  ) but I don’t want to ever travel on the same path twice. Can I begin at my home, visit every path, and then return? The data representation in all of these cases is the graph!

Terms Graph – set of vertices and set of edges. More formally, a graph is an ordered pair of finite sets V and E. The set V is the set of vertices. The set E is the set of edges. Sometimes the edges have weight (termed a weighted graph) Sometimes the edges have direction (I can go from A to B but not B to A). A graph with directed edges is termed a digraph or directed graph. Otherwise, we call it an undirected graph. Successor – the follows relationship in a directed graph. Degree – the number of edges incident to or touching a vertex. In-degree – the number of edges coming into a vertex (digraph only). Out-degree – the number of edges going out of a vertex (digraph only). Predecessor – the precedes relationship in a digraph.

Draw a graph of five people around you now. The people become the nodes. Let edges be “is born in the same year as you”. Is this directed or undirected? Is in connected? (A connected graph is a graph that you can get from a node to every other node following edges

Examples of Graphs “Want to work with” V={0,1,2,3,4} E={(0,1), (1,2), (0,3), (3,0), (2,2), (4,3)} 1 When (x,y) is an edge, we say that x is adjacent to y, and y is adjacent from x. 0 is adjacent to 1. 1 is not adjacent to 0. 2 is adjacent from 1. 2 4 3

A “Real-life” Example of a Graph V=set of 6 people: John, Mary, Joe, Helen, Tom, and Paul, of ages 12, 15, 12, 15, 13, and 13, respectively. E ={(x,y) | if x is younger than y} Mary Helen Joe John Tom Paul

Intuition Behind Graphs The nodes represent entities (such as people, cities, computers, words, etc.) Edges (x,y) represent relationships between entities x and y, such as: “x loves y” “x hates y” “x is a friend of y” (note that this not necessarily reciprocal) “x considers y a friend” “x is a child of y” “x is a half-sibling of y” “x is a full-sibling of y” In those examples, each relationship is a different graph

Graph Representation For graphs to be computationally useful, they have to be conveniently represented in programs There are two computer representations of graphs: Adjacency matrix representation Adjacency lists representation

Adjacency Matrix Representation In this representation, each graph of n nodes is represented by an n x n matrix A, that is, a two-dimensional array A The nodes are (re)-labeled 1,2,…,n A[i][j] = 1 if (i,j) is an edge A[i][j] = 0 if (i,j) is not an edge

Example of Adjacency Matrix 1 0 1 0 1 0 0 0 1 0 0 1 0 0 0 0 0 0 0 4 0 A = 2 4 3

Another Example of Adj. Matrix Re-label the nodes with numerical labels Mary 0 Helen 1 0 0 0 0 0 0 1 1 0 0 1 1 1 1 0 0 0 0 A = Joe 3 John 2 Tom 4 Paul 5

Pros and Cons of Adjacency Matrices Simple to implement Easy and fast to tell if a pair (i,j) is an edge: simply check if A[i][j] is 1 or 0 Cons: No matter how few edges the graph has, the matrix takes O(n2) in memory

Adjacency Lists Representation A graph of n nodes is represented by a one-dimensional array L of linked lists, where L[i] is the linked list containing all the nodes adjacent from node i. The nodes in the list L[i] are in no particular order

Example of Linked Representation L[0]: empty L[1]: empty L[2]: 0, 1, 4, 5 L[3]: 0, 1, 4, 5 L[4]: 0, 1 L[5]: 0, 1 Helen 1 Mary 0 Joe 3 John 2 Tom 4 Paul 5

class EdgeNode { public: int toNode; // Node id of target of edge int fromNode; // Node id of source of edge int distance; // Cost of Edge (for a weighted graph only) EdgeNode *next; // Next edge in linked list for adjacency list representation // Constructor – note the use of default parameters. // Note the strange assignments (distance(dist)) – they aren’t my favorite either, but texts seem to like them EdgeNode( int from = -1, int to=-1, int distance=0, EdgeNode *nextEdge = NULL): distance(dist),toNode( to ), fromNode(from), next(nextEdge){}; }; // EdgeNode

class GraphNode { public: inf nodeID; // ID of node; something that makes it easy to find the node in the array string name; // Node name EdgeNode *adj; // Adjacency list bool visited; // true if visited already GraphNode( int id= -1, string nameEle=" ", EdgeNode *adjList = NULL): nodeID(id), name(nameEle), adj(adjList){ visited = false;}  }; // GraphNode   class Graph { protected: GraphNode *G; // Array of nodes of graph – will be given space once the size is known int nodeCt; // Size of G public: Graph(int size ) { G = new GraphNode[size]; nodeCt = size;}; // create node array string toString(string label,ostream & fout); void BuildGraph(istream & inf); }; // Graph

Pros and Cons of Adjacency Lists Saves on space (memory): the representation takes as many memory words as there are nodes and edge. Cons: It can take up to O(n) time to determine if a pair of nodes (i,j) is an edge: one would have to search the linked list L[i], which takes time proportional to the length of L[i].

Directed vs. Undirected Graphs If the directions of the edges matter, then we show the edge directions, and the graph is called a directed graph (or a digraph) If the relationships represented by the edges are symmetric (such as (x,y) is edge if and only if x is a sibling of y), then we don’t show the directions of the edges, and the graph is called an undirected graph.

Examples of Undirected Graphs V=set of 6 people: John, Mary, Joe, Helen, Tom, and Paul, where the first 4 are siblings, and the last two are siblings E ={(x,y) | x and y are siblings} if (x,y) is an edge: we say that x is adjacent to y, & y adjacent to x. We also say that x and y are neighbors Mary Helen Joe John Tom Paul

Representations of Undirected Graphs The same two representations for directed graphs can be used for undirected graphs Adjacency matrix A: A[i][j]=1 if (i,j) is an edge; 0 otherwise Adjacency Lists: L[i] is the linked list containing all the neighbors of i

Example of Representations Mary 0 Helen 1 Linked Lists: L[0]: 1, 2, 3 L[1]: 0, 2, 3 L[2]: 0, 1, 3 L[3]: 0, 1, 2 L[4]: 5 L[5]: 4 John 2 Joe 3 Tom 4 Paul 5 Adjacency Matrix: 0 1 1 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 A =

Definition of Some Graph Related Concepts Let G be a directed graph The indegree of a node x in G is the number of edges coming to x The outdegree of x is the number of edges leaving x. Let G be an undirected graph The degree of a node x is the number of edges that have x as one of their end nodes The neighbors of x are the nodes adjacent to x

Things for You To Do Add a member function to the class graph, called getIndegree( int x), which returns the indegree of node x Add a member function to the class graph, called getOutdegree( int x), which returns the outdegree of node x

Paths A path in a graph G is a sequence of nodes x1, x2, …,xk, such that there is an edge from each node the next one in the sequence For example, in the first example graph, the sequence 3, 0, 1, 2 is a path, but the sequence 0, 3, 4 is not a path because (0,3) is not an edge In the “sibling-of” graph, the sequence John, Mary, Joe, Helen is a path, but the sequence Helen, Tom, Paul is not a path

Graph Connectivity An undirected graph is said to be connected if there is a path between every pair of nodes. Otherwise, the graph is disconnected Informally, an undirected graph is connected if it hangs in one piece Connected Disconnected

Connected Components If an undirected graph is not connected, then each “piece” is called a connected component. A piece in itself is connected, but if you bring any other node to it from the graph, it is no longer connected If the graph is connected, then the whole graph is one single connected component Of Interest: Given any undirected graph G, Is G connected? If not, find its connected components.

Suppose the graph below represents one way streets in a town and the nodes represent hotels. Maybe I only care about hotels 0,1,3,6 in the figure. I could take those nodes and the edges between them and talk about the subgraph.   Figure Subgraph – a graph that consists of a subset of vertices and a subset of edges. Normally, the edges are all edges between the vertices and no others.

By definition a graph does not contain: Multiple copies of the same edge. Self-edges – edges of the form . This is also called a loop. If we need multiple edges between nodes, it is termed a multigraph. In our hotel example, multiple edges between 6 and 5 could represent two different road between the hotels. Sometimes we want to talk about a sequence of edges: Path – a sequence of edges (one ends where next begins) Simple path – there are no repeated vertices or edges. Length of a path – the sum of the lengths of the edges on the path. Sometimes we want to talk about cycles (a path that begins and ends at the same place) Simple cycle – a cycle with no repeated vertices (except at the beginning and the end). The cycle (0320) in the graph above is a simple cycle. Connected component – A component is a subgraph in which for every pair of vertices in the component, there exists some path that connects them. Connected graph – there is a path between every pair of vertices in the graph. This implies that a graph with n vertices must have at least edges.

Graph Traversal Techniques The previous connectivity problem, as well as many other graph problems, can be solved using graph traversal techniques There are two standard graph traversal techniques: Depth-First Search (DFS) Breadth-First Search (BFS)

Depth First Search Backtrack to 10,6,7 9, 11, 8, 5 Backtrack to 8,11 Output List: 1 2 4 3 7 6 10 13 9 11 8 5 15 14 12 1 1 2 2 5 5 8 8 11 11 15 15 6 6 10 10 13 13 3 3 4 4 7 7 9 9 12 12 14 14 Start with 1 2,4,3 Stop at 3 Backtrack to 4, can we search? 7,6 Stop at 2 Backtrack to 6, can we search? 10,13 Stop at 13 Backtrack to 10,6,7 9, 11, 8, 5 Stop at 5 Backtrack to 8,11 15,14,12 STOP – All nodes are marked!!

Depth First Search – Real Life Application From xkcd.com

Graph Traversal (Contd.) In both DFS and BFS, the nodes of the undirected graph are visited in a systematic manner so that every node is visited exactly once. Both BFS and DFS consider an embeded tree: When a node x is visited, it is labeled as visited, and it is added to the tree If the traversal got to node x from node y, y is viewed as the parent of x, and x a child of y

Depth-First Search DFS follows the following rules: Select an unvisited node x, visit it, and treat as the current node Find an unvisited neighbor of the current node, visit it, and make it the new current node; If the current node has no unvisited neighbors, backtrack to the its parent, and make that parent the new current node; Repeat steps 3 and 4 until no more nodes can be visited. If there are still unvisited nodes, repeat from step 1.

Illustration of DFS 1 2 1 4 9 4 5 7 10 2 11 9 8 5 6 7 11 Graph G 6 8 1 2 1 4 9 4 5 7 10 2 11 9 8 5 6 7 11 Graph G 6 8 10 DFS Tree

At seats Write recursive code to do a DFS search

Breadth-First Search BFS follows the following rules: Select an unvisited node x, visit it, have it be the root in a BFS tree being formed. Its level is called the current level. From each node z in the current level, in the order in which the level nodes were visited, visit all the unvisited neighbors of z. The newly visited nodes from this level form a new level that becomes the next current level. Repeat step 2 until no more nodes can be visited. If there are still unvisited nodes, repeat from Step 1.

Illustration of BFS 1 2 2 4 1 4 9 5 9 10 5 7 10 11 8 6 7 8 11 6 Graph G BFS Tree

Breadth First Search L0: 1 L1: 2, 3 L2: 4,5,6,11 L3: 7,8,10,9,15 13 13 3 3 4 4 7 7 9 9 12 12 14 14 L0: 1 L1: 2, 3 L2: 4,5,6,11 L3: 7,8,10,9,15 L4: 13,12,14 Final output: 1, 2, 3, 4, 5, 6, 11, 7, 8, 10, 9, 15, 13, 12, 14

Topological Ordering In college, before taking a particular course, students usually must take all applicable prerequisite courses. For example, before taking the Programming II course, students must take the Programming I course. However, certain courses can be taken independently of each other. The courses within a department can be represented as a directed graph. A directed edge from, say, vertex u to vertex v means that the course represented by the vertex u is a prerequisite of the course represented by the vertex v. It would be helpful for students to know, before starting a major, the sequence in which they can take courses so that before taking a particular course they take all its prerequisite courses.

We assume that the graph has no cycles. Let G be a directed graph and V(G) = {v1, v2, ..., vn}, where n > 0. A topological ordering of V(G) is a linear ordering vi1, vi2, ..., vin of the vertices such that if vij is a predecessor of vik, j  k, 1  j  n, and 1  k  n, then vij precedes vik, that is, j < k in this linear ordering. We assume that the graph has no cycles. Because the graph has no cycles: There exists a vertex u in G such that u has no predecessor. There exists a vertex v in G such that v has no successor.

Suppose that the array topologicalOrder (of size n, the number of vertices) is used to store the vertices of G in topological order. Thus, if a vertex, say u, is a successor of the vertex v and topologicalOrder[j]= v and topologicalOrder[k] = u, then j < k. The topological sort algorithm can be implemented using either the depth first traversal or the breadth first traversal. This section discusses how to implement topological ordering using the breadth first traversal.

In the breadth first topological ordering we first find a vertex that has no predecessor vertex and place it first in the topological ordering. We next find the vertex, say v, all of whose predecessors have been placed in the topological ordering and place v next in the topological ordering. To keep track of the number of vertices of a vertex we use the array predCount. Initially, predCount[j] is the number of predecessors of the vertex vj. The queue used to guide the breadth first traversal is initialized to those vertices vk such that predCount[k] is zero.

The general algorithm is:

The vertices of G3 in breadth first topological ordering are 0 9 1 7 2 5 4 6 3 8 10 We illustrate the breadth first topological ordering of the graph G3