Example #1
0
        internal static GeometryGraph CreateAndLayoutGraph()
        {
            double w = 30;
            double h = 20;
            GeometryGraph graph = new GeometryGraph();
            Node a = new Node( new Ellipse(w, h, new P()),"a");
            Node b = new Node( CurveFactory.CreateRectangle(w, h, new P()),"b");
            Node c = new Node( CurveFactory.CreateRectangle(w, h, new P()),"c");
            Node d = new Node(CurveFactory.CreateRectangle(w, h, new P()), "d");

            graph.Nodes.Add(a);
            graph.Nodes.Add(b);
            graph.Nodes.Add(c);
            graph.Nodes.Add(d);
            Edge e = new Edge(a, b) { Length = 10 };
            graph.Edges.Add(e);
            graph.Edges.Add(new Edge(b, c) { Length = 3 });
            graph.Edges.Add(new Edge(b, d) { Length = 4 });

            //graph.Save("c:\\tmp\\saved.msagl");
            var settings = new Microsoft.Msagl.Layout.MDS.MdsLayoutSettings();
            LayoutHelpers.CalculateLayout(graph, settings, null);

            return graph;
        }
Example #2
0
 private Edge(Edge prev)
 {
     qPrev = prev;
     prev.qNext = this;
     LFace = null;
     Token = 0;
 }
Example #3
0
        public void test3()
        {
            UndirectedGraph<int, Edge<int>> graph = new UndirectedGraph<int, Edge<int>>(true);

            // Add vertices to the graph
            graph.AddVertex(0);
            graph.AddVertex(1);
            graph.AddVertex(2);
            graph.AddVertex(3);
            graph.AddVertex(4);
            // Create the edges
            Edge<int> e0_1 = new Edge<int>(0, 1);
            Edge<int> e0_2 = new Edge<int>(0, 2);
            Edge<int> e1_4 = new Edge<int>(1, 4);
            Edge<int> e3_4 = new Edge<int>(3, 4);
            Edge<int> e0_3 = new Edge<int>(0, 3);
            Edge<int> e1_2 = new Edge<int>(1, 2);
            // Add the edges
            graph.AddEdge(e0_1);
            graph.AddEdge(e0_2);
            graph.AddEdge(e1_2);
            graph.AddEdge(e1_4);
            graph.AddEdge(e3_4);
            graph.AddEdge(e0_3);

            List<int> path = new List<int>();

            HamiltonianDefiner definer = new HamiltonianDefiner(graph);
            bool isHamiltonian = definer.isHamiltonianGraph(path);
            Assert.AreEqual(isHamiltonian, true);
        }
Example #4
0
        /// <summary>
        /// Class constructor.
        /// </summary>
        
        /// <date>2013-07-23</date>
        public HalfEdge(Edge objEdge, Point objSiteLeft, Point objSiteRight)
        {
            this.Site = objSiteLeft;
            this.Edge = objEdge;

            // 'angle' is a value to be used for properly sorting the
            // halfsegments counterclockwise. By convention, we will
            // use the angle of the line defined by the 'site to the left'
            // to the 'site to the right'.
            // However, border edges have no 'site to the right': thus we
            // use the angle of line perpendicular to the halfsegment (the
            // edge should have both end points defined in such case.)
            if (objSiteRight != null)
            {
                this.Angle = Math.Atan2(objSiteRight.y - objSiteLeft.y, objSiteRight.x - objSiteLeft.x);
            }
            else
            {
                if (objEdge.SiteLeft == objSiteLeft)
                {
                    this.Angle = Math.Atan2(objEdge.VertexB.x - objEdge.VertexA.x, objEdge.VertexA.y - objEdge.VertexB.y);
                }
                else
                {
                    this.Angle = Math.Atan2(objEdge.VertexA.x - objEdge.VertexB.x, objEdge.VertexB.y - objEdge.VertexA.y);
                }
            }
        }
Example #5
0
        public IEnumerable<int> FindTourStartingFrom(Edge startEdge)
        {
            this.InitializeVisit();

            this.VisitNode(startEdge.Head);
            yield return startEdge.Head;

            this.VisitNode(startEdge.Tail);
            yield return startEdge.Tail;

            int currentNode = startEdge.Tail;

            TaskLogger.Text = "Computing nearest neighbor tour...";

            while (unvisitedNodes.ItemsCount > 0)
            {
                int nearestUnvisitedNode = this.FindNearestUnvisitedNode(currentNode);

                this.VisitNode(nearestUnvisitedNode);
                yield return nearestUnvisitedNode;

                currentNode = nearestUnvisitedNode;

                TaskLogger.Progress = 100.0 * visitedNodes.ItemsCount / nodes.Length;
            }
        }
        /// <summary>
        /// Creates stub edges for all the intersections in this
        /// Edge (if any) and inserts them into the graph.
        /// </summary>
        /// <param name="edge"></param>
        /// <param name="l"></param>
        public virtual void ComputeEdgeEnds(Edge edge, IList l)
        {
            EdgeIntersectionList eiList = edge.EdgeIntersectionList;       
            // ensure that the list has entries for the first and last point of the edge
            eiList.AddEndpoints();

            IEnumerator it = eiList.GetEnumerator();
            EdgeIntersection eiPrev = null;
            EdgeIntersection eiCurr = null;
            // no intersections, so there is nothing to do
            if (! it.MoveNext()) return;
            EdgeIntersection eiNext = (EdgeIntersection) it.Current;
            do 
            {
                eiPrev = eiCurr;
                eiCurr = eiNext;
                eiNext = null;
                
                if (it.MoveNext())
                    eiNext = (EdgeIntersection)it.Current;                

                if (eiCurr != null) 
                {
                    CreateEdgeEndForPrev(edge, l, eiCurr, eiPrev);
                    CreateEdgeEndForNext(edge, l, eiCurr, eiNext);
                }
            } 
            while (eiCurr != null);
        }
 public EdgeViewModel(int index, Edge edge, BoundaryCondition condition)
 {
     Index = index;
     Edge = edge;
     Condition = condition;
     Types = new ObservableCollection<BoundaryConditionsType>(getTypesOfBoundaryConditions());
 }
        public NeuralNetwork(int countInputNodes, int countOutputNodes, int countHiddenNodes)
        {
            input_nodes = new List<Node>();
            hidden_nodes = new List<Node>();
            output_nodes = new List<Node>();

            // add input nodes
            for (int i = 0; i < countInputNodes; i++)
                input_nodes.Add(new Node());
            // add hidden nodes
            for (int i = 0; i < countHiddenNodes; i++)
                hidden_nodes.Add(new Node());
            // add output nodes
            for (int i = 0; i < countOutputNodes; i++)
                output_nodes.Add(new Node());

            // create edges from data to input
            for (int i = 0; i < input_nodes.Count; i++)
                input_nodes[i].AddEdge(true, new Edge(null, input_nodes[i], 0));

            // create edges from input to hidden
            Edge newEdge;
            for(int i  =0; i < input_nodes.Count; i++)
                for(int j = 0; j < hidden_nodes.Count; j++)
                {
                    newEdge = new Edge(input_nodes[i], hidden_nodes[j], 0);
                }

            // create edges from hidden to output

            // create edges from output to output data
        }
Example #9
0
        public EdgeList(Edge edge)
        {
            _edges = new List<Edge>();
            _edges.Add(edge);

            _weight = edge.Weight;
        }
Example #10
0
        public static void Main(string[] args)
        {
            Console.Title = "Graph";

            Vertex jana = new Vertex();
            Vertex philip = new Vertex();
            Vertex markus = new Vertex();

            Edge relMarkusPhilip = new Edge();
            Edge relJanaPhilip= new Edge();

            jana.Data.Add("Class", "Person");
            philip.Data.Add("Class", "Person");
            markus.Data.Add("Class", "Person");

            jana.Data.Add("Name", "Jana");
            philip.Data.Add("Name", "Philip");
            markus.Data.Add("Name", "Markus");

            relJanaPhilip.Data.Add("Class", "Friend");
            relMarkusPhilip.Data.Add("Class", "Friend");

            relMarkusPhilip.ConnectVertices(ref markus, ref philip);
            relJanaPhilip.ConnectVertices(ref jana, ref philip);

            List<Edge> results = markus.SearchVertices("Friend");

            foreach (Edge e in results)
                Console.WriteLine(e.Data["Class"]);

            Console.ReadLine();
        }
Example #11
0
 public void Add( Edge edge )
 {
     if( !TryAdd( edge ) )
     {
         throw new ArgumentException( "Edge already exists: " + edge.Id );
     }
 }
        public void FindShortestPathForSimpleUndirectedGraphUsingDijkstraAlgorithm()
        {
            var graph = new UndirectedGraph<object, Edge<object>>(true);
            object v1 = "vertex1";
            object v2 = "vertex2";
            object v3 = "vertex3";
            var e1 = new Edge<object>(v1, v2);
            var e2 = new Edge<object>(v2, v3);
            var e3 = new Edge<object>(v3, v1);
            graph.AddVertex(v1);
            graph.AddVertex(v2);
            graph.AddVertex(v3);
            graph.AddEdge(e1);
            graph.AddEdge(e2);
            graph.AddEdge(e3);

            var algorithm = new UndirectedDijkstraShortestPathAlgorithm<object, Edge<object>>(graph, edge => (double)1);
            var observer = new UndirectedVertexPredecessorRecorderObserver<object, Edge<object>>();
            using (observer.Attach(algorithm))
            {
                algorithm.Compute(v1);
            }

            IEnumerable<Edge<object>> path;
            observer.TryGetPath(v3, out path);

            foreach (var edge in path)
            {
                Console.WriteLine(edge);
            }
        }
Example #13
0
		public static Graph Read(StreamReader stream)
		{
			int.Parse(stream.ReadLine());
			int E = int.Parse(stream.ReadLine());

			var graph = new Graph();

			for(int i = 0; i < E; ++i)
			{
				string[] r = stream.ReadLine().Trim().Split(' ');
				r = r.Where (x => x.Length > 0).ToArray();

				var edge = new Edge()
				{
					From = int.Parse(r[0]),
					To = int.Parse(r[1]),
					Weight = float.Parse(r[2]),
				};

				if(!graph.Vertices.ContainsKey(edge.From))
				{
					graph.Vertices[edge.From] = new List<Edge>();
				}
				graph.Vertices[edge.From].Add(edge);
			}

			return graph;
		}
        /// <summary>
        /// create a geometry edge, the geometry source and target have to be set already
        /// </summary>
        /// <param name="drawingEdge"></param>
        /// <param name="msaglGraph"></param>
        /// <returns></returns>
        static Core.Layout.Edge CreateGeometryEdgeAndAddItToGeometryGraph(Edge drawingEdge, GeometryGraph msaglGraph) {
            var msaglEdge = CreateGeometryEdgeFromDrawingEdge(drawingEdge);

            msaglGraph.Edges.Add(msaglEdge);
            
            return msaglEdge;
        }
        public void FisrstSampleInput()
        {
            int nodesCount = 4;

            var edges = new List<Edge>
            {
                new Edge(0, 1, 9),
                new Edge(0, 3, 4),
                new Edge(3, 1, 6),
                new Edge(3, 2, 11),
                new Edge(1, 2, 5)
            };

            var expectedEdges = new Edge[]
            {
                edges[1],
                edges[4],
                edges[2]
            };

            var actualEdges = KruskalAlgorithm.Kruskal(nodesCount, edges);

            CollectionAssert.AreEqual(expectedEdges, actualEdges);
            Assert.AreEqual(15, actualEdges.Sum(e => e.Weight));
        }
        protected override void OnMouseClick(MouseEventArgs e)
        {
            var x = Convert.ToInt32(Math.Floor(e.X/pixelDelta));
            var y = Convert.ToInt32(Math.Floor(e.Y/pixelDelta));
            var clickPoint = new Point(x, y);

            mousePoint = clickPoint;
            edgeFirstPoint = clickPoint;

            if (currentPolygon == null)
                currentPolygon = new Polygon(clickPoint, PanelColor.BackColor);
            else
            {
                currentPolygon.TryAddVertex(clickPoint);
                if (currentPolygon.Closed)
                {
                    polygons.Add(currentPolygon);
                    currentPolygon = null;
                    edgeFirstPoint = null;
                    currentEdge = null;
                }
            }

            Invalidate();
        }
Example #17
0
 public Halfedge(Type _lock,Edge edge=null,LR lr=null)
 {
     if(_lock!=typeof(PrivateConstrutorEnforcer)){
         Debug.LogError("halfedge constructor is private");
     }
     init(edge,lr);
 }
        public void AddExploredEdgeToPath1(Edge edge)
        {
            this.Path1.AddExploredEdge(edge);

            SearchEvent ev = new SearchEvent(SearchEventType.AddedEdgeToExplored, edge);
            this.SEM.AddEvent(ev);
        }
 private static Point2DCollection ClipRing(Point2DCollection ring, Edge edge)
 {
     if ((ring == null) || (ring.Count < 2))
     {
         return null;
     }
     Point2DCollection points = new Point2DCollection();
     Point2D lastPoint = ring[ring.Count - 1];
     for (int i = 0; i < ring.Count; i++)
     {
         Point2D point = ring[i];
         if (Inside(point, edge))
         {
             if (Inside(lastPoint, edge))
             {
                 points.Add(point);
             }
             else
             {
                 Point2D item = EdgeIntersection(lastPoint, point, edge);
                 points.Add(item);
                 points.Add(point);
             }
         }
         else if (Inside(lastPoint, edge))
         {
             Point2D item = EdgeIntersection(lastPoint, point, edge);
             points.Add(item);
         }
         lastPoint = point;
     }
     return points;
 }
        public void AddTraveledEdge(Edge edge)
        {
            this.TraveledEdges.Add(edge);

            SearchEvent ev = new SearchEvent(SearchEventType.AddedEdgeToTraveled, edge);
            this.EventManager.AddEvent(ev);
        }
        public Algorithm(List<List<int>> input)
        {
            // Create cities
            cities = new Vertex[input[0][0]];

            for (int i = 0; i < input[0][0]; i++)
            {
                cities[i] = new Vertex();
            }

            // Create ways
            ways = new List<Edge>();

            // and other stuff
            linked = new List<int>();
            done = new List<int>();
            routes = new List<List<int>>();

            for (int i = 1; i < input.Count; i++)
            {
                Edge way = new Edge();

                way.weight = input[i][0];
                way.u = input[i][1];
                way.v = input[i][2];

                ways.Add(way);
            }
        }
        public void RemoveExplored(Edge edge)
        {
            this.ExploredEdges.Remove(edge);

            SearchEvent ev = new SearchEvent(SearchEventType.RemovedEdgeFromExplored, edge);
            this.EventManager.AddEvent(ev);
        }
        public static Area CreateArea()
        {
            Area area = new Area();

            Node node0 = new Node(area, new Vector2(500, 50));
            Node node1 = new Node(area, new Vector2(50, 50));
            Node node2 = new Node(area, new Vector2(300, 70));
            Node node3 = new Node(area, new Vector2(400, 140));
            Node node4 = new Node(area, new Vector2(90, 300));
            Node node5 = new Node(area, new Vector2(450, 300));
            Node node6 = new Node(area, new Vector2(600, 400));
            Node node7 = new Node(area, new Vector2(150, 200));

            Edge edge0 = new Edge(area, node0, node3, 1);
            Edge edge1 = new Edge(area, node1, node2, 1);
            Edge edge2 = new Edge(area, node4, node3, 3);
            Edge edge3 = new Edge(area, node6, node5, 3);
            Edge edge4 = new Edge(area, node3, node5, 2);
            Edge edge5 = new Edge(area, node1, node4, 1);
            Edge edge6 = new Edge(area, node2, node7, 1);
            Edge edge7 = new Edge(area, node7, node1, 3);
            Edge edge8 = new Edge(area, node2, node3, 2);
            Edge edge9 = new Edge(area, node6, node3, 3);
            Edge edge10 = new Edge(area, node6, node4, 1);
            Edge edge11 = new Edge(area, node0, node6, 3);

            return area;
        }
 /// <summary>
 ///
 /// </summary>
 /// <param name="e"></param>
 public MonotoneChainEdge(Edge e)
 {
     _e = e;
     _pts = e.Coordinates;
     MonotoneChainIndexer mcb = new MonotoneChainIndexer();
     _startIndex = mcb.GetChainStartIndices(_pts);
 }
 ///<summary>Creates a new WrapperEdge.<summary>
 ///<param name="edge">The edge to wrap.</param>
 ///<param name="SubscribeToEdge">Should this subscribe to the edge.</param>
 public WrapperEdge(Edge edge, bool SubscribeToEdge) {
   _weclosed = 0;
   _edge = edge;
   if(SubscribeToEdge) {
     _edge.Subscribe(this, null);
   }
 }
        /// <summary>
        /// populate the geometry including curve and arrowhead positioning for the given edge using simple
        /// straight line routing style.  Self edges will be drawn as a loop, padding is used to control the
        /// size of the loop.
        /// </summary>
        /// <param name="edge">edge to route</param>
        /// <param name="padding">controls size of loop</param>
        public static void RouteEdge(Edge edge, double padding)
        {
            ValidateArg.IsNotNull(edge, "edge");

            var eg = edge.EdgeGeometry;

            if (eg.SourcePort == null)
            {
#if SHARPKIT // Lambdas bind differently in JS
                eg.SourcePort = ((Func<Edge,RelativeFloatingPort>)(ed => new RelativeFloatingPort(() => ed.Source.BoundaryCurve,
                    () => ed.Source.Center)))(edge);
#else
                eg.SourcePort = new RelativeFloatingPort(() => edge.Source.BoundaryCurve, () => edge.Source.Center);
#endif
            }

            if (eg.TargetPort == null)
            {
#if SHARPKIT // Lambdas bind differently in JS
                eg.TargetPort = ((Func<Edge, RelativeFloatingPort>)(ed => new RelativeFloatingPort(() => ed.Target.BoundaryCurve,
                    () => ed.Target.Center)))(edge);
#else
                eg.TargetPort = new RelativeFloatingPort(() => edge.Target.BoundaryCurve, () => edge.Target.Center);
#endif
            }

            if (!ContainmentLoop(eg, padding))
            {
                eg.Curve = GetEdgeLine(edge);
            }

            Arrowheads.TrimSplineAndCalculateArrowheads(eg, eg.SourcePort.Curve,
                                                         eg.TargetPort.Curve, edge.Curve, false, false);
                      
        }
Example #27
0
 public ETClient(Edge e)
 {
   _sync = new object();
   _sent_blocks = new Hashtable();
   _e = e;
   _e.Subscribe(this, null );
 }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="e"></param>
 public MonotoneChainEdge(Edge e)
 {
     this.e = e;
     pts = e.Coordinates;
     MonotoneChainIndexer mcb = new MonotoneChainIndexer();
     startIndex = mcb.GetChainStartIndices(pts);
 }
Example #29
0
 private bool Equals(Edge obj)
 {
     if (Direction == EdgeDirection.Undirected)
     {
         return (obj != null && 
                     (
                         (
                         obj.Vertex1.Equals(this.Vertex1) &&
                         obj.Vertex2.Equals(this.Vertex2)
                         ) ||
                         (
                         obj.Vertex1.Equals(this.Vertex2) &&
                         obj.Vertex2.Equals(this.Vertex1)
                         )
                     ) &&
             obj.Type.Equals(this.Type) &&
             obj.Relationship.Equals(this.Relationship));
     }
     else
     {
         return (obj != null &&
                 obj.Vertex1.Equals(this.Vertex1) &&
                 obj.Vertex2.Equals(this.Vertex2) &&
                 obj.Type.Equals(this.Type) &&
                 obj.Relationship.Equals(this.Relationship));
     }
 }
 ///<summary>Makes the SecurityOverlord listen to the edge and instantiates
 ///a new SecurityAssociation for the insecure edge.  CreateSecurityAssociation
 ///is idempotent.</summary>
 protected override void WrapEdge(Edge edge) {
   edge.Subscribe(_so, null);
   SecurityAssociation sa = _so.CreateSecurityAssociation(edge, DefaultEdgeSPI, !edge.IsInbound);
   if(edge.IsClosed) {
     sa.Close("Edge closed too quickly.");
   }
 }