/
Graph Data Structures Graph Data Structures

Graph Data Structures - PowerPoint Presentation

olivia-moreira
olivia-moreira . @olivia-moreira
Follow
346 views
Uploaded On 2020-01-26

Graph Data Structures - PPT Presentation

Graph Data Structures Unless in communicating with it a computer one says exactly what one means trouble is bound to result Alan Turing Early Graph Theory Problem Leonhard Euler 1707 1783 ID: 773911

data graph structures 321 graph data 321 structures edge vertex adjacency vertices matrix edges number private undirected graphs list

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Graph Data Structures" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Graph Data Structures "Unless in communicating with it [a computer] one says exactly what one means, trouble is bound to result. " - Alan Turing

Early Graph Theory Problem Leonhard Euler (1707 - 1783)One of the first mathematicians to study graphsThe Seven Bridges of Konigsberg ProblemA puzzle for the residents of the city The river Pregel flows through the city7 bridges cross the riverCan you cross all bridges while crossing each bridge only once? CS 321 - Data Structures

Konigsberg and the River Pregel CS 321 - Data Structures

Question 1 How many solutions does the Seven Bridges of Konigsberg Problem have?012 3 >= 4 CS 321 - Data Structures

Question 1 How many solutions does the Seven Bridges of Konigsberg Problem have?012 3 >= 4 CS 321 - Data Structures

How to Solve? Euler's SolutionRedraw the map as a graph CS 321 - Data Structures

Euler's Proposal Euler tour - traverse each edge only once and return to starting pointA connected graph has an Euler tour if and only if every vertex has an even number of edges. CS 321 - Data Structures

Definition A graph G consists of two setsa finite, nonempty set of vertices V a finite, possible empty set of edges E G(V,E) represents a graph CS 321 - Data Structures G 1 V(G 1 )={0,1,2,3} E(G 1 )={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3 )} 0 2 1 3

Definition An undirected graph is one in which the pair of vertices in a edge is unordered ( v 0 , v 1 ) = (v 1 ,v 0 ) CS 321 - Data Structures G 2 V(G 2 )={0,1,2,3,4,5,6} E(G 2 )={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6 )} 0 1 2 3 4 5 6

Definition A directed graph (or digraph) is one in which each edge is a directed pair of vertices( v 0 , v 1 ) != (v 1 ,v0) For the edge (v 0 , v 1 ) , the start vertex v 0 is called the tail and the end vertex v 1 is called the head. CS 321 - Data Structures tail head G 3 V(G 3 )={0,1,2} E(G 3 )={(0,1 ) ,(1,0 ) ,(1,2 ) } 0 1 2

Arpanet Computer Scientists use graphs to model all kinds of things CS 321 - Data Structures A rpanet 1969, 1971

CS 321 - Data Structures Roman Land Transportation Network

Enron Emails CS 321 - Data Structures

US Airport Network CS 321 - Data Structures

Facebook CS 321 - Data Structures

Graph Definitions In a weighted graph the edge has cost or weight that measures the cost of traveling along the edge CS 321 - Data Structures

Paths and Cycles A path is a sequence of vertices connected by edgesThe path length is the number of edges The weighted path length is the sum of the cost of the edges in a pathA simple path is a path in which all vertices are distinct A cycle is a path of length 1 or more that starts and ends at the same vertexa directed acyclic graph (DAG) is a directed graph with no cycles CS 321 - Data Structures

Connected Graph In an undirected graph G, two vertices, v 0 and v 1 , are connected if there is a path in G from v0 to v1An undirected graph is connected if, for every pair of distinct vertices vi , v j , there is a path from v i to vjCS 321 - Data Structures

Complete Graph A complete graph is a graph that has the maximum number of edgesfor undirected graph with n vertices, the maximum number of edges is . for directed graph with n vertices, the maximum number of edges is .   CS 321 - Data Structures

How to store a graph as a data structure? Representing Graphs CS 321 - Data Structures

Graph Representation Adjacency MatrixAdjacency ListsCS 321 - Data Structures

Adjacency Matrix Let G=(V,E) be a graph with n vertices.The adjacency matrix of G is a two-dimensional n by n array, say adj_mat If the edge (v i, vj) is in E , adj_mat [ i ][j]=1 If there is no such edge in E , adj_mat [ i ][j]=0 The adjacency matrix for an undirected graph is symmetric The adjacency matrix for a digraph need not be symmetric CS 321 - Data Structures

Example of Adjacency Matrix CS 321 - Data Structures For undirected graphs the matrix is symmetric.

Analysis of Adjacency Matrix CS 321 - Data StructuresIn a graph with m edges and n vertices: if the graph is undirected, the number of 1 ’s in the matrix is 2*m i f the graph is directed, the number of 1’s in the matrix is mthe amount of memory required is O(n2 ) for weighted graphs, replace Boolean values with weight of an edge The cost of determining whether an edge ( v i ,v j ) belongs to the graph is O(1)

Adjacency List Given a graph with n vertices, can represent it as an array adj_list of n lists adj_list [ i ] , contains the list of vertice s adjacent to the vertex v i i n a directed graph, a vertex vj is adjacent to vertex vi if the edge ( v i ,v j ) is in E i n an undirected graph, for each edge (v i,vj) in E, vj is adjacent to vi and vi is adjacent to vj for weight graph, store vertex and weight of an edge in the list.   CS 321 - Data Structures

Examples of Adjacency Lists CS 321 - Data Structures

Analysis of Adjacency List CS 321 - Data StructuresIn a graph with m edges and n vertices: if the graph is undirected, the sum of the lengths of all lists is 2*m i f the graph is directed , the sum of the lengths of all lists is mthe amount of memory required for adjacency-list representation is O(n+m) for both directed and undirected graphs.the cost to determine whether an edge ( v i ,v j ) belongs to the graph is O(m ) .

Sparse / Dense Graphs In a dense graph, the number of edges is large.close to the maximum number of edges. In a sparse graph, the number of edges is much less than the maximum possible number of edges No standard cutoff for dense and sparse graphs CS 321 - Data Structures

Which Graph Representation? For dense graphs, the adjacency matrix is more efficient.most of the matrix contains useful information. But most graphs are sparse, not dense. For sparse graphs, an adjacency list is more efficient. no wasted space . CS 321 - Data Structures

CS 321 - Data Structures

Graph Implementation public class Graph private static final double INFINITY = Double.MAX_VALUE ; private Map<String, Vertex> vertices; public Graph() // create empty Graph public void addEdge (String source, String dest , double cost) // find all paths from given vertex public void findUnweightedShortestPath (String startName ) // called after findUnweightedShortestPath public void printPath (String destName ) CS 321 - Data Structures

Vertex Class (nested in Graph)CS 321 - Data Structures private static class Vertex private String name; private List<Edge> adjacent; public Vertex(String n) // for shortest path algorithms private double distance; private Vertex prev ; private int scratch; // call before finding new paths public void reset()

Edge Class (nested in Graph) CS 321 - Data Structuresprivate static class Edge private Vertex dest ; private double cost ; private Edge(Vertex d, double c)

Representing a Map CS 321 - Data Structures

Adjacency Matrix    A Br Bl Ch Co E FG G Pa Pe S U V A   0 1  1  1  0  0 0  0 1  0  0 1 0 Br 1 0  1  0  1  0 1  1 1  1  1 1 1  Bl 1 1  0  1  0  0 0  0 1  1  0 0 0 Ch 1 0  1  0  0  0 0  0 0  1  0 0 0 Co 0 1  0  0  0  1 0  0 0  1  0 0 1 E   0 0  0  0  1  0 0  0 0  1  0 0 0  FG 0 1  0  0  0  0 0  0 0  0  1 0 0 G   0 1  0  0  0  0 0  0 0  0  1 0 1 Pa 1 1  1  0  0  0 0  0 0  0  0 0 0 Pe 0 1  1  1  1  1 0  0 0  0  0 0 0 S   0 1  0  0  0  0 1  1 0  0  0 0 0 U   1 1  0  0  0  0 0  0 0  0  0 0 0 V   0 1  0  0  1  0 0  1 0  0  0 0 0 Country Code Argentina A Brazil Br Bolivia Bl Chile Ch Columbia Co Ecuador E French Guiana FG Guyana G Paraguay Pa Peru Pe Suriname S Uruguay U Venezuela V CS 321 - Data Structures

The Map Coloring Problem How many colors do you need to color a map, so that no 2 countries that have a common border (not a point) are colored the same?How to solve using Brute Force? CS 321 - Data Structures

A Solution Green Green Green Blue Yellow Blue Yellow Blue Yellow Yellow Blue Red CS 321 - Data Structures

What About the Ocean? A Br Bl Ch Co E FG G Pa Pe S U V Oc A  0 1  1  1  0  0 0  0 1  0  0 1 0 1 Br 1 0  1  0  1  0 1  1 1  1  1 1 1 1  Bl 1 1  0  1  0  0 0  0 1  1  0 0 0 0  Ch 1 0  1  0  0  0 0  0 0  1  0 0 0 1 Co 0 1  0  0  0  1 0  0 0  1  0 0 1 1 E  0 0  0  0  1  0 0  0 0  1  0 0 0 1  FG 0 1  0  0  0  0 0  0 0  0  1 0 0 1 G  0 1  0  0  0  0 0  0 0  0  1 0 1 1 Pa 1 1  1  0  0  0 0  0 0  0  0 0 0 0 Pe 0 1  1  1  1  1 0  0 0  0  0 0 0 1 S  0 1  0  0  0  0 1  1 0  0  0 0 0 1 U  1 1  0  0  0  0 0  0 0  0  0 0 0 1 V  0 1  0  0  1  0 0  1 0  0  0 0 0 1 Oc 1 1 0 1 1 1 1 1 0 1 1 1 1 0 CS 321 - Data Structures

Make the Ocean Blue Green Green Green Blue Yellow Blue Yellow Blue Yellow Yellow Red CS 321 - Data Structures Red Red Red