80 likes | 158 Views
Explore the basics of graphs, paths, and graph algorithms in computer science. Learn about different types of graphs, paths, cycles, and connectivity. Discover graph properties, algorithms, and applications.
E N D
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 cheapest way to get from Pittsburgh to St Louis? • Electric Circuits • Circuit elements - transistors, resistors, capacitors • is everything connected together? • Depends on interconnections (wires) • If this circuit is built will it work? • Depends on wires and objects they connect. • Job Scheduling • Interconnections indicate which jobs to be performed before others • When should each task be performed • All these questions can be answered using a mathematical object named a “graph” • what are graphs? • what are their basic properties?
Introduction to Graphs • Graph • A set of vertices(nodes) V = {v1, v2, …., vn} • A set of edges(arcs) that connects the vertices E={e1, e2, …, em} • Each edge ei is a pair (v, w) where v, w in V • |V| = number of vertices (cardinality) • |E| = number of edges • Graphs can be • directed (order (v,w) matters) • Undirected (order of (v,w) doesn’t matter) • Edges can be • weighted (cost associated with the edge) • eg: Neural Network, airline route map(vanguard airlines)
Paths and Graphs • A path is a sequence of edges from one node to another • A length of a path is the number of edges • If w1, w2, …, wn is a sequence of vertices such that (wi, wi+1) in E, then there is a path of length n-1 from w1 to wn. • what are paths of length 2 in vanguard airlines map? • A simple path is a path where no vertex is repeated • first and last vertices can be the same • what is an example of a simple path in the vanguard map? • What is an example of a non-simple path? • A cycle (in a directed graph) is a path that begins and ends in the same vertex. • i.e. S = {w1, w2, …, wn} is a sequence such that w1 = wn and |S| >1 • A directed acyclic graph (DAG) is a directed graph having no cycles.
More Graph Definitions • Connected Graph • A undirected graph is connected if for all (u,v) in V, there exists a path from u to v. • A directed graph is strongly connected if for all (u,v) in V, there exists a path from u to v. • A directed graph is weakly connected if for all (u,v) in V, either (u,v) is in E or (v,u) is in E. • Complete Graph • A graph with all nodes connected to each other directly • Maximal Edge count • undirected graph |E|max = (n-1) + (n-2) + … + 2 + 1 = n(n-1)/2 • directed graph |E|max = n(n-1) • Degree • number of edges incident to a vertex • in a directed graph • in-degree(sink) - number of edges into vertex • out-degree(source) - number of edges from vertex
Some Cardinality Relations • For most graphs |E| |V|2 • A dense graph when most edges are present • E = (|V|2) • A dense graph • large number of edges (quadratic) • Also |E| > |V| log |V| can be considered dense • A sparse graph is a graph with relatively few edges • no clear definition • metric for sparsity |E| < |V| log |V| • eg: ring computer network
More About Graphs • A graph with no cycles is called a tree. • This is a general definition of a tree • A group of disconnected trees is called a “forest” • A spanning tree of a graph is a subgraph that contains all vertices but only enough of the edges to form a tree. • Any additional edge to a tree will form a cycle • A tree with V vertices has exactly V-1 edges (induction proof) A A C G C G B B D D E E F F
Graph Algorithms • Graphs depend on two parameters • edges (E) • Vertices (V) • Graph algorithms can be complicated to analyze • One algorithm might be order (V2) • for dense graphs • Another might be order((E+V)log E) • for sparse graphs • Depth First Search • Is the graph connected? If not what are their connected components? • Does the graph have a cycle? • How do we examine (visit) every node and every edge systematically? • First select a vertex, set all vertices connected to that vertex to non-zero • Find a vertex that has not been visited and repeat the step above • Repeat above until all zero vertices are examined.